<?php
/**
 * @Author: Marte
 * @Date:   2019-09-20 09:43:37
 * @Last Modified by:   Marte
 * @Last Modified time: 2019-12-16 17:15:13
 */

namespace Home\Controller;
use Home\Controller\CommunicationController;
use Home\Controller\JscxController;
use User\Api\UserApi;
header("Content-Type: text/html;charset=utf-8");
/**
 * 文档模型控制器
 * 文档模型列表和详情
 */
class MechanismController extends HomeController {
    /**
     * 检测是否登录
     * */
    public function _initialize()
    {
        $action_name=ACTION_NAME;
        $this->assign('action_name',$action_name);
        if($action_name == Login || $action_name == login){
            //登录 跟首页接口的时候不需要判断是否登录
        }elseif(empty(session('me_id'))){
            $this->error('请登录',U('Mechanism/Login'));
        }else{
            $me_id=session('me_id');
            $lp_list=M('floor')->where("me_id=$me_id")->select();
            $this->assign('lp_list',$lp_list);
            //再来判断权限
            if(session('Supertube')==1){
                $person_name=M('mechanism')->where("id=$me_id")->getField('person_name');
                $this->assign('person_name',$person_name);
                $floor_id=session('floor_id');
                $this->assign('fff_id',$floor_id);
                //超管 拥有所有权限
            }else{
                $staff_id=session('staff_id');
                //不是超管只能看自己楼盘的相关数据
                $person_name=M('department_staff')->where("id=$staff_id")->getField('staff_name');
                $this->assign('person_name',$person_name);
                $floor_id=session('floor_id');
                $lp_list=M('floor')->where("id=$floor_id")->select();
                $this->assign('fff_id',$floor_id);
                $this->assign('lp_list',$lp_list);
                //判断权限
                $permission_role=M('department_staff')->where("id=$staff_id")->getField('permission_role');
                $jurisdiction = M('jurisdiction')->where("id=$permission_role")->getField('qx');
                $action_name = ACTION_NAME;
                if ($action_name == index || $action_name == Index) {
                    //访问数据统计
                    $domain = strstr($jurisdiction, ',1,');
                    if (!$domain) {
                        $this->error('没有访问权限');
                    }
                } else if ($action_name == department_manage || $action_name == Department_manage) {
                    //排行榜
                    $domain = strstr($jurisdiction, ',2,');
                    if (!$domain) {
                        $this->error('没有访问权限');
                    }
                } else if ($action_name == staff || $action_name == Staff) {
                    //排行榜
                    $domain = strstr($jurisdiction, ',3,');
                    if (!$domain) {
                        $this->error('没有访问权限');
                    }
                } else if ($action_name == role_manage || $action_name == Role_manage) {
                    //排行榜
                    $domain = strstr($jurisdiction, ',4,');
                    if (!$domain) {
                        $this->error('没有访问权限');
                    }
                } else if ($action_name == punch_record || $action_name == Punch_record) {
                    //排行榜
                    $domain = strstr($jurisdiction, ',5,');
                    if (!$domain) {
                        $this->error('没有访问权限');
                    }
                } else if ($action_name == floor_index || $action_name == Floor_index) {
                    //排行榜
                    $domain = strstr($jurisdiction, ',6,');
                    if (!$domain) {
                        $this->error('没有访问权限');
                    }
                } else if ($action_name == house_type || $action_name == House_type) {
                    //排行榜
                    $domain = strstr($jurisdiction, ',7,');
                    if (!$domain) {
                        $this->error('没有访问权限');
                    }
                } else if ($action_name == build_manage || $action_name == Build_manage) {
                    //排行榜
                    $domain = strstr($jurisdiction, ',8,');
                    if (!$domain) {
                        $this->error('没有访问权限');
                    }
                } else if ($action_name == souse_res || $action_name == Souse_res) {
                    //排行榜
                    $domain = strstr($jurisdiction, ',9,');
                    if (!$domain) {
                        $this->error('没有访问权限');
                    }
                } else if ($action_name == news_index || $action_name == News_index) {
                    //排行榜
                    $domain = strstr($jurisdiction, ',10,');
                    if (!$domain) {
                        $this->error('没有访问权限');
                    }
                } else if ($action_name == class_index || $action_name == Class_index) {
                    //排行榜
                    $domain = strstr($jurisdiction, ',11,');
                    if (!$domain) {
                        $this->error('没有访问权限');
                    }
                } else if ($action_name == activity_index || $action_name == Activity_index) {
                    //排行榜
                    $domain = strstr($jurisdiction, ',12,');
                    if (!$domain) {
                        $this->error('没有访问权限');
                    }
                } else if ($action_name == sign_up || $action_name == Sign_up) {
                    //排行榜
                    $domain = strstr($jurisdiction, ',13,');
                    if (!$domain) {
                        $this->error('没有访问权限');
                    }
                } else if ($action_name == good_index || $action_name == Good_index) {
                    //排行榜
                    $domain = strstr($jurisdiction, ',14,');
                    if (!$domain) {
                        $this->error('没有访问权限');
                    }
                } else if ($action_name == good_type || $action_name == Good_type) {
                    //排行榜
                    $domain = strstr($jurisdiction, ',15,');
                    if (!$domain) {
                        $this->error('没有访问权限');
                    }
                } else if ($action_name == exchange_manage || $action_name == Exchange_manage) {
                    //排行榜
                    $domain = strstr($jurisdiction, ',16,');
                    if (!$domain) {
                        $this->error('没有访问权限');
                    }
                } else if ($action_name == data_management || $action_name == Data_management) {
                    //排行榜
                    $domain = strstr($jurisdiction, ',17,');
                    if (!$domain) {
                        $this->error('没有访问权限');
                    }
                } else if ($action_name == statistical_data || $action_name == Statistical_data) {
                    //排行榜
                    $domain = strstr($jurisdiction, ',18,');
                    if (!$domain) {
                        $this->error('没有访问权限');
                    }
                } else if ($action_name == achievement || $action_name == Achievement) {
                    //排行榜
                    $domain = strstr($jurisdiction, ',19,');
                    if (!$domain) {
                        $this->error('没有访问权限');
                    }
                } else if ($action_name == conversion || $action_name == Conversion) {
                    //排行榜
                    $domain = strstr($jurisdiction, ',20,');
                    if (!$domain) {
                        $this->error('没有访问权限');
                    }
                } else if ($action_name == browse || $action_name == Browse) {
                    //排行榜
                    $domain = strstr($jurisdiction, ',21,');
                    if (!$domain) {
                        $this->error('没有访问权限');
                    }
                } else if ($action_name == housing_statistics || $action_name == Housing_statistics) {
                    //排行榜
                    $domain = strstr($jurisdiction, ',22,');
                    if (!$domain) {
                        $this->error('没有访问权限');
                    }
                } else if ($action_name == user_list || $action_name == User_list) {
                    //排行榜
                    $domain = strstr($jurisdiction, ',23,');
                    if (!$domain) {
                        $this->error('没有访问权限');
                    }
                } else if ($action_name == agent || $action_name == Agent) {
                    //排行榜
                    $domain = strstr($jurisdiction, ',24,');
                    if (!$domain) {
                        $this->error('没有访问权限');
                    }
                } else if ($action_name == system || $action_name == System) {
                    //排行榜
                    $domain = strstr($jurisdiction, ',25,');
                    if (!$domain) {
                        $this->error('没有访问权限');
                    }
                } else if ($action_name == system_customer || $action_name == System_customer) {
                    //排行榜
                    $domain = strstr($jurisdiction, ',26,');
                    if (!$domain) {
                        $this->error('没有访问权限');
                    }
                }

            }
        }
    }



    public function index(){
        $id=session('me_id');
        if(!$id){
            return $this->error('请登录',U('login'));
        }

        $info=M('mechanism')->where("id=$id")->find();
        if($info['logo']){
        	$info['logo']=M('picture')->where("id=$info[logo]")->getfield('path');
        }
        $this->assign('info',$info);
        $this->assign('cur','信息设置');
        $this->assign('type',1);
        $this->display();
    }
    //保存基本信息
    public function save_xx(){
        $id=session('me_id');
        $save_data['logo']=$_POST['img'];
        $save_data['name']=$_POST['name'];
        $save_data['introduction']=$_POST['introduction'];
        // print_r($save_data);
        $res=M('mechanism')->where("id=$id")->save($save_data);
        if($res){
            $data['result'] = 1;
            echo json_encode($data);
        }else{
            $data['result'] = 2;
            echo json_encode($data);
        }
    }
    public function login(){
    	if($_POST){
    		$map['phone']=$_POST['phone'];
            $map['password']=$this->think_ucenter_md5($_POST['password'], '@gBeM[(lqcLzSduvpOY}~KH-i6?G47W%V1wy+;5D');
    		$res=M('mechanism')->where($map)->find();
    		if($res){
    			//第一次登录的时候没有楼盘信息什么的
                session("Supertube",1);
                session("me_info",$res);
                session("me_id",$res['id']);
                $floor_id=M('floor')->where("me_id=$res[id]")->order('id asc')->getField('id');
                session('floor_id',$floor_id);
                $data['result'] = 1;
                $data['msg'] = '成功';
                echo json_encode($data);exit;

    			
    		}elseif($_POST['code']){
                //管理员登录用密码  员工登录用手机验证码
    			$map['code']=$_POST['code'];
    			$mobile=$_POST['phone'];
    			$result = M('sms_code')->where(array('phone'=>$mobile))->order('create_time desc')->find();
    			if($result['failure_time']<time()){
                    $data['result'] = 2;
                    $data['msg'] = '验证码已过期';
                    echo json_encode($data);exit;
                	// return $this->error('验证码已过期',U('login'));
            	}elseif($code = $result['code']){
            		//查看是否有这个手机号码  员工表去查的
            		$role=M('department_staff')->where("mobile=$mobile")->getfield('role');
            		if($role){
            			//存在这个手机号码，他是什么角色 有无权限
            			$qx=M('jurisdiction')->where("role=$id")->getfield('qx');
            			if($qx){
            				//根据权限去显示内容
                            $me_id=M('department_staff')->where("mobile=$mobile")->getfield('me_id');
                            $staff_id=M('department_staff')->where("mobile=$mobile")->getfield('id');
                            $floor_id=M('department_staff')->where("mobile=$mobile")->getfield('floor_id');
            				$me_info=M('mechanism')->where("id=$me_id")->find();
            				//如果这个Supertube等于2就是要去看有哪些权限
                            session("Supertube",2);
			                session("me_info",$me_info);
                            session("me_id",$me_id);
                            session("floor_id",$floor_id);
                            session("staff_id",$staff_id);//员工ID  现在是想员工跟着楼盘走 而不是跟着机构走，是不是员工登录就只能看到对应的楼盘信息
                            $data['result'] = 1;
                            $data['msg'] = '成功';
                            echo json_encode($data);exit;
			                // return $this->success('成功',U('index'));
            			}else{
                            $data['result'] = 2;
                            $data['msg'] = '暂无权限';
                            echo json_encode($data);exit;
            				// return $this->error('暂无权限',U('login'));
            			}
            		}
            	}else{
                    $data['result'] = 2;
                    $data['msg'] = '验证码错误';
                    echo json_encode($data);exit;
            		// return $this->error('验证码错误',U('login'));
            	}
    			
    		}else{
                $data['result'] = 2;
                $data['msg'] = '账号不存在或者密码错误';
                echo json_encode($data);exit;
    			// return $this->error('账号不存在或者密码错误');
    		}
    	}
    	$this->display();
    }

    function think_ucenter_md5($str, $key = 'ThinkUCenter'){
        return '' === $str ? '' : md5(sha1($str) . $key);
    }
    public function outLogin(){
        session_destroy();
        return $this->success('退出成功',U('login'));
    }

    public function zh_floor(){
        $action_name=I('ac_name');
        $floor_id=I('id');
        session('floor_id',$floor_id);
        $value=['action_name'=>$action_name];
        echo json_encode($value);exit;
    }


            //发送短信
    public function send_sms($mobile = ''){
        $input = I('post.');
        if(empty($mobile)) $mobile = $input['mobile'];

        //获取手机号
        $pattern = "/^1[123456789]\d{9}$/";
        if (empty($mobile)) {
            return api_return(2,'请输入手机号');
        } else {
            if (preg_match($pattern, $mobile)) {

                //tzb
                // $count = M('ucenter_member')->where(['mobile' => $mobile])->count();
                // if($count>0){
                //     return api_return(2,'此号码已注册');
                // }

                $result = M('sms_code')->where(array('phone'=>$mobile))->order('create_time desc')->find();
                $result = time()-$result['create_time'];
                if ($result < 60 and $result>0) {
                    return api_return(2,'当前手机号操作频繁,请60秒后发送');
                }
                $authnum = rand(100000,999999);
                $statusStr = array(
                    "0" => 1,
                    "-1" => "参数不全",
                    "-2" => "服务器空间不支持,请确认支持curl或者fsocket，联系您的空间商解决或者更换空间！",
                    "30" => "密码错误",
                    "40" => "账号不存在",
                    "41" => "余额不足",
                    "42" => "帐户已过期",
                    "43" => "IP地址限制",
                    "50" => "内容含有敏感词"
                );
                $smsapi = "http://api.smsbao.com/";
                $user = 'SZY123456'; //短信平台帐号
                $pass = md5('Yang1413'); //短信平台密码
                $content = "【楼盘】尊敬的用户您的验证码" . $authnum . "如非本人操作，请忽略。";//要发送的短信内容
                $phone = $mobile;//要发送短信的手机号码
                $sendurl = $smsapi . "sms?u=" . $user . "&p=" . $pass . "&m=" . $phone . "&c=" . urlencode($content);
                $result = file_get_contents($sendurl);
                if ($result == 0) {
                    $info['code'] = $authnum;
                    $info['phone'] = $phone;
                    $info['create_time'] = time();
                    $info['failure_time'] = time()+600;//失效时间：10分钟
                    M('sms_code')->add($info);
                    return api_return(1,'短信发送成功',$authnum);
                } else {
                    return api_return(2,$statusStr[$result]);
                }
            } else {
                return api_return(2,'手机号码格式错误');
            }
        }
    }


    //机构认证信息  现在是三个页面
    public function authentication_information(){
        $id=session('me_id');
        // if($_POST){
        // 	$menu=$_REQUEST['menu'];
        //     $id=$_REQUEST['id'];
        //     if($menu==1){
        //         $save_data['name']=$_REQUEST['name'];
        //         $save_data['logo']=$_REQUEST['logo'];
        //         $save_data['introduction']=$_REQUEST['introduction'];
        //         $res=M('mechanism')->where("id=$id")->save($save_data);
        //     }elseif($menu==2){
        //         $save_data['full_name']=$_REQUEST['full_name'];
        //         $save_data['person_name']=$_REQUEST['person_name'];
        //         $save_data['person_mobile']=$_REQUEST['person_mobile'];
        //         $save_data['id_card_z']=$_REQUEST['id_card_z'];
        //         $save_data['id_card_f']=$_REQUEST['id_card_f'];
        //         $save_data['business_license']=$_REQUEST['business_license'];
        //         $res=M('mechanism')->where("id=$id")->save($save_data);
        //     }else{
        //         $old_password=$this->think_ucenter_md5($_REQUEST['old_password'], '@gBeM[(lqcLzSduvpOY}~KH-i6?G47W%V1wy+;5D');
        //         $password=M('mechanism')->where("id=$id")->getfield('password');
        //         if($password != $old_password){
        //             return $this->error('原密码错误',U('Mechanism/authentication_information',array('menu'=>3)));
        //         }
        //         $new_password=$_REQUEST['new_password'];
        //         $confirm_password=$_REQUEST['confirm_password'];
        //         if($new_password != $confirm_password){
        //             return $this->error('新密码跟确认密码不一致',U('Mechanism/authentication_information',array('menu'=>3)));
        //         }
        //         $save_data['password']=$_REQUEST['new_password'];
        //         $res=M('mechanism')->where("id=$id")->save($save_data);
        //     }
        //     if($res){
        //         $this->success('成功',U('Mechanism/authentication_information'));
        //     }else{
        //         $this->error('失败',U('Mechanism/authentication_information'));
        //     }

        // }else{
        	$info=M('mechanism')->where("id=$id")->find();
	        if($info['id_card_z']){
	        	$info['id_card_z']=M('picture')->where("id=$info[id_card_z]")->getfield('path');
	        }
	        if($info['id_card_f']){
	        	$info['id_card_f']=M('picture')->where("id=$info[id_card_f]")->getfield('path');
	        }
	        if($info['business_license']){
	        	$info['business_license']=M('picture')->where("id=$info[business_license]")->getfield('path');
	        }
	        $this->assign('info',$info);
            $this->assign('cur','信息设置');
            $this->assign('type',2);
	        $this->display();
        // }

    }
    //保存认证信息
    public function save_authentication(){
        $id=session('me_id');
        $save_data['full_name']=$_POST['full_name'];
        $save_data['person_name']=$_POST['person_name'];
        $save_data['person_mobile']=$_POST['person_mobile'];
        $save_data['id_number']=$_POST['id_number'];
        $save_data['id_card_z']=$_POST['id_card_z'];
        $save_data['id_card_f']=$_POST['id_card_f'];
        $save_data['business_license']=$_POST['business_license'];
        // print_r($save_data);
        $res=M('mechanism')->where("id=$id")->save($save_data);
        if($res){
            $data['result'] = 1;
            echo json_encode($data);
        }else{
            $data['result'] = 2;
            echo json_encode($data);
        }
    }
    //修改密码
    public function update_password(){
    	$id=session('me_id');
        if(!$id){
            return $this->error('请登录',U('login'));
        }
        // if($_POST){
        // 	$data=$_POST;
        //     //原密码
        // 	$password=M('mechanism')->where("id=$id")->getfield('password');
        //     //原密码
        //     $password1=$this->think_ucenter_md5($_POST['password'], '@gBeM[(lqcLzSduvpOY}~KH-i6?G47W%V1wy+;5D');

        //     //新密码
        //     $new_password=$_POST['new_password'];
        //     //确认密码
        //     $confirm_password=$_POST['confirm_password'];

        // 	if($password !== $password1){
        // 		return $this->error('原密码错误',U('update_password'));
        // 	}elseif($data['new_password'] !== $data['confirm_password']){
        // 		return $this->error('密码跟确认密码不一致',U('update_password'));
        // 	}else{
        // 		$res=M('mechanism')->where("id=$id")->save(['password'=>$data['new_password']]);
        // 		if($res){
        // 			return $this->success('成功',U('update_password'));
        // 		}else{
        // 			return $this->error('失败',U('update_password'));
        // 		}
        // 	}
        // }
        $this->assign('cur','信息设置');
        $this->assign('type',3);
        $this->display();
    }
    //操作修改密码
    public function save_password(){
         $id=session('me_id');
         $data=$_POST;
            //原密码
         $password=M('mechanism')->where("id=$id")->getfield('password');
            //原密码
            $password1=$this->think_ucenter_md5($_POST['password'], '@gBeM[(lqcLzSduvpOY}~KH-i6?G47W%V1wy+;5D');

            //新密码
            $new_password=$_POST['new_password'];
            //确认密码
            $confirm_password=$_POST['confirm_password'];

         if($password !== $password1){
             // return $this->error('原密码错误',U('update_password'));
            $data['result'] = 2;
            $data['msg']='原密码错误';
            echo json_encode($data);
         }elseif($data['new_password'] !== $data['confirm_password']){
             // return $this->error('密码跟确认密码不一致',U('update_password'));
            $data['result'] = 2;
            $data['msg']='新密码跟确认密码不一致';
            echo json_encode($data);
         }else{
             $np=$this->think_ucenter_md5($_POST['new_password'], '@gBeM[(lqcLzSduvpOY}~KH-i6?G47W%V1wy+;5D');
             $res=M('mechanism')->where("id=$id")->save(['password'=>$np]);
             if($res){
             $data['result'] = 1;
             $data['msg']='成功';
            echo json_encode($data);
             }else{
             $data['result'] = 2;
             $data['msg']='失败';
            echo json_encode($data);
             }
         }
    }
    //部门管理 //默认是第一个楼盘  可以切换楼盘 对应的部门 员工都会变
    public function department_manage(){
    	$id=session('me_id');
    	$floor_id=session('floor_id');
        //先找出第一个楼盘  我设计换楼盘的时候把楼盘ID存到缓存里   如果是员工登录，直接就把楼盘ID存缓存了
        if(!$floor_id){
        	//如果没有floor_id，说明没有切换楼盘，取第一个楼盘
        	$floor_id=M('floor')->where("me_id=$id")->order('id asc')->getfield('id');
        }
        $map['is_delete']=array('neq',1);
        $map['floor_id']=$floor_id;
        $_REQUEST['name']?$map['name']=array('like',"%$_REQUEST[name]%"):'';
        $this->assign('name',$_REQUEST['name']);
        $count = M('department')->where($map)->count();
        $Page = new \Think\Page($count, 10,$_REQUEST);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $d_list=M('department')->limit($Page->firstRow . ',' . $Page->listRows)->where($map)->order('id desc')->select();

        foreach($d_list as $k=>$v){
        	//部门人数
        	$d_list[$k]['count']=M('department_staff')->where("department_id=$v[id]")->count();
            $d_list[$k]['create_time']=date('Y-m-d H:i:s',$v['create_time']);
        }
        $show = $Page->show();// 分页显示输出
        $this->assign("_page", $show);
        $this->assign('d_list',$d_list);
        $this->assign('cur','部门管理');
        $this->display();
    }
    //新增部门
    public function department_add(){
        if($_POST && empty($_POST['id'])){
            $data['name']=$_POST['name'];//部门名称
            $data['func']=$_POST['func'];//职能
            
            $floor_id=session('floor_id');
            $me_id=session('me_id');
            //先找出第一个楼盘  我设计换楼盘的时候把楼盘ID存到缓存离   如果是员工登录 登录的时候就把floor_id存缓存了
            if(!$floor_id){
                //如果没有floor_id，说明没有切换楼盘，取第一个楼盘
                $floor_id=M('floor')->where("me_id=$me_id")->order('id asc')->getfield('id');
            }
            //先找出第一个楼盘  我设计换楼盘的时候把楼盘ID存到缓存离   如果是员工登录 登录的时候就把floor_id存缓存了
            if(!$floor_id){
                //如果没有floor_id，说明没有切换楼盘，取第一个楼盘
                $floor_id=M('floor')->where("me_id=$me_id")->order('id asc')->getfield('id');
            }
            $data['floor_id']=$floor_id;
            $data['create_time']=time();
            $res=M('department')->add($data);
            if($res){
                return $this->success('成功',U('department_manage'));
            }else{
                return $this->success('失败',U('department_manage'));
            }
        }elseif($_POST['id']){
            $data['name']=$_REQUEST['name'];//部门名称
            $data['func']=$_REQUEST['func'];//职能
            $data['id']=$_POST['id'];
 
            $res=M('department')->save($data);
            if($res){
                return $this->success('成功',U('department_manage'));
            }else{
                return $this->success('失败',U('department_manage'));
            }
        }
        //$me_id=session('me_id');//机构名称
        //$floor_id=M('floor')->where("me_id=$me_id")->select();//机构下的楼盘，部门跟楼盘楼盘走 可能最后需求又是跟着机构走
        $id=$_REQUEST['id'];
        if($id){
            $info=M('department')->where("id=$id")->find();
            $this->assign('info',$info);
        }
        $this->assign('cur','部门管理');
        $this->display();
    }
    //删除部门
    public function department_del(){
        $id=$_REQUEST['id'];
        $res=M('department')->where("id=$id")->save(['is_delete'=>1]);
        if($res){
            return $this->success('成功',U('department_manage'));
        }
    }
    //员工管理   没有再部门下面显示有点意外，不分部门感觉很乱
    public function staff(){
        $floor_id=session('floor_id');
        $me_id=session('me_id');
        //先找出第一个楼盘  我设计换楼盘的时候把楼盘ID存到缓存离   如果是员工登录 登录的时候就把floor_id存缓存了
        if(!$floor_id){
            //如果没有floor_id，说明没有切换楼盘，取第一个楼盘
            $floor_id=M('floor')->where("me_id=$me_id")->order('id asc')->getfield('id');
        }
        $map['floor_id']=$floor_id;
        $map['is_del']=array('eq',0);
        $_REQUEST['staff_name']?$map['staff_name']=array('like',"%$_REQUEST[staff_name]%"):'';
        $this->assign('staff_name',$_REQUEST['staff_name']);
        $count = M('department_staff')->where($map)->count();
        $Page = new \Think\Page($count, 10,$_REQUEST);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $list=M('department_staff')->limit($Page->firstRow . ',' . $Page->listRows)->where($map)->order('id desc')->select();
        foreach($list as $k=>$v){
        	$list[$k]['department_name']=M('department')->where("id=$v[department_id]")->getfield('name');
        	$list[$k]['create_time']=date('Y-m-d H:i:s',$v['create_time']);
            if($v['role']==1){
                $list[$k]['role']='置业顾问';
            }else{
                $list[$k]['role']='管理人员';
            }
            if($v['status']==1){
                $list[$k]['status']='正常';
            }else{
                $list[$k]['status']='离职';
            }
        }
        $show = $Page->show();// 分页显示输出
        $this->assign("_page", $show);
        $this->assign('cur','员工管理');
        $this->assign('list',$list);
        $this->display();
    }
    //新增员工
    public function staff_add(){
        $id=session('me_id');
        $floor_id=session('floor_id');
        //先找出第一个楼盘  我设计换楼盘的时候把楼盘ID存到缓存里   如果是员工登录，直接就把楼盘ID存缓存了
        if(!$floor_id){
            //如果没有floor_id，说明没有切换楼盘，取第一个楼盘
            $floor_id=M('floor')->where("me_id=$id")->order('id asc')->getfield('id');
        }
        if($_POST && empty($_POST['id'])){
            $data['staff_name']=$_REQUEST['staff_name'];
            $data['img']=$_REQUEST['img'];
            $data['mobile']=$_REQUEST['mobile'];
            $data['address']=$_REQUEST['address'];
            $data['sex']=$_REQUEST['sex'];
            $data['department_id']=$_REQUEST['department_id'];
            $data['role']=$_REQUEST['role'];
            $data['status']=$_REQUEST['status'];
            $data['create_time']=time();
            //楼盘ID  不确定是选中楼盘，还是直接当前楼盘
            $data['floor_id']=$floor_id;
            $data['me_id']=session('me_id');
            $res=M('department_staff')->add($data);
            if($res){
                return $this->success('成功',U('staff'));
            }else{
                return $this->error('失败',U('staff'));
            }
            
        }elseif($_POST['id']){
            //编辑员工
            $data['staff_name']=$_REQUEST['staff_name'];
            $data['img']=$_REQUEST['img'];
            $data['mobile']=$_REQUEST['mobile'];
            $data['address']=$_REQUEST['address'];
            $data['sex']=$_REQUEST['sex'];
            $data['department_id']=$_REQUEST['department_id'];
            $data['role']=$_REQUEST['role'];
            $data['status']=$_REQUEST['status'];
            $data['create_time']=time();
            //楼盘ID  不确定是选中楼盘，还是直接当前楼盘
            // $data['floor_id']=$floor_id;
            // $data['me_id']=session('me_id');
            $data['id']=$_REQUEST['id'];
            $res=M('department_staff')->save($data);
            if($res){
                return $this->success('成功',U('staff'));
            }else{
                return $this->error('失败',U('staff'));
            }
        }
        //取出这个楼盘的部门
        $de_list=M('department')->where("floor_id=$floor_id")->order('id desc')->select();
        //取出这个楼盘的角色  可能是跟着机构走，还不确定(现在是写死，只分成置业顾问跟管理人员)
        $role_list=M('jurisdiction')->where("floor_id=$floor_id")->order('id desc')->select();

        $iid=I('id');
        if($iid){
            $info=M('department_staff')->where("id=$iid")->find();
            if($info['img']){
                $info['img']=M('picture')->where("id=$info[img]")->getfield('path');
            }
            
            $this->assign('info',$info);
        }
        $this->assign('de_list',$de_list);
        $this->assign('role_list',$role_list);
        $this->assign('cur','员工管理');
        $this->display();
    }
    //员工删除
    public function staff_del(){
        $id=I('id');
        $res=M('department_staff')->where("id=$id")->save(['is_del'=>1]);
        if($res){
            return $this->success('成功',U('staff'));
        }else{
            return $this->error('失败',U('staff'));
        }
    }
    //积分管理(弄错了，我取的是用户的积分明细，应该要取员工的积分明细)
    public function point_manage(){
        $id=$_REQUEST['id'];//员工ID
        $map['de_id']=$id;
        // $count = M('department_staff')->where($map)->count();
        $count=M('employee_points')->where($map)->count();
        $Page = new \Think\Page($count, 10,$_REQUEST);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $list=M('employee_points')->limit($Page->firstRow . ',' . $Page->listRows)->where($map)->order('id desc')->select();

        foreach($list as $k=>$v){
            $list[$k]['create_time']=date('Y-m-d H:i:s',$v['create_time']);
            if($v['reason']==1){
                $list[$k]['reason_name']='上访未更新';
            }elseif($v['reason']==2){
                $list[$k]['reason_name']='认筹和认购未更新';
            }elseif($v['reason']==3){
                $list[$k]['reason_name']='录入信息';
            }elseif($v['reason']==4){
                $list[$k]['reason_name']='邀约来访';
            }elseif($v['reason']==5){
                $list[$k]['reason_name']='认筹';
            }elseif($v['reason']==6){
                $list[$k]['reason_name']='认购';
            }elseif($v['reason']==7){
                $list[$k]['reason_name']='签约';
            }

            if($v['type']==1){
                $list[$k]['type_name']='+';
            }else{
                $list[$k]['type_name']='-';
            }
        }

        $sum=M('department_staff')->where("id=$id")->getfield('point');//这个积分有点奇怪，每个月清零
        // print_r($list);die;
        $show = $Page->show();// 分页显示输出
        $this->assign("_page", $show);
        $this->assign('cur','员工管理');
        $this->assign('list',$list);
        $this->assign('sum',$sum);
        $this->display();
    }
    //查看汇报（还没弄，看置业顾问端怎么弄，我直接取数据）
    public function view_report(){
        $p_list=M('point')->select();
        $this->assign('p_list',$p_list);
        $this->assign('cur','员工管理');
        $this->display();
    }
    //新增权限角色
    public function role_add(){
        $id=session('me_id');
        $floor_id=session('floor_id');


        //现在我是照着权限根据楼盘来弄 可能是跟机构
        if($_POST && empty($_POST['id'])){
            $data['me_id']=session('me_id');
            $data['floor_id']=session('floor_id');
            $data['role_name']=$_REQUEST['role_name'];
            $data['status']=$_REQUEST['status'];
            $data['func']=$_REQUEST['func'];

            //这里肯定是要处理的，到时看页面具体看怎么操作
            // $data['qx']=$qx;

            $jurisdiction = $_REQUEST['checked'];
            foreach ($jurisdiction as $k => $v) {
                $jur .= ',' . $v;
            }
            // $add_data['jurisdiction']=substr($jur, 1);//后面觉得还是前面有个逗号好
            $data['qx'] = $jur;
            $data['create_time']=time();

            $res=M('jurisdiction')->add($data);
            if ($res) {
                return api_return(1, '成功');
            } else {
                return api_return(2, '失败');
            }
        }elseif($_POST['id']){
            $data['me_id']=session('me_id');
            $data['floor_id']=session('floor_id');
            $data['role_name']=$_REQUEST['role_name'];
            $data['status']=$_REQUEST['status'];
            $data['func']=$_REQUEST['func'];
            $data['id']=$_REQUEST['id'];
            //这里肯定是要处理的，到时看页面具体看怎么操作
            $jurisdiction = $_REQUEST['checked'];
            foreach ($jurisdiction as $k => $v) {
                $jur .= ',' . $v;
            }
            // $add_data['jurisdiction']=substr($jur, 1);//后面觉得还是前面有个逗号好
            $data['qx'] = $jur;

            $res=M('jurisdiction')->save($data);
            if ($res) {
                return api_return(1, '成功');
            } else {
                return api_return(2, '失败');
            }
        }


        $id = $_REQUEST['id'];
        if($id){
            $info = M('jurisdiction')->where("id=$id")->find();

            $jur = substr($info['qx'], 1);
            $jurisdiction = explode(',', $jur);
            foreach ($jurisdiction as $k => $v) {
                if ($v == 1) {
                    $a = 1;
                } else if ($v == 2) {
                    $b = 2;
                } else if ($v == 3) {
                    $c = 3;
                } else if ($v == 4) {
                    $d = 4;
                } else if ($v == 5) {
                    $e = 5;
                } else if ($v == 6) {
                    $f = 6;
                } else if ($v == 7) {
                    $g = 7;
                } else if ($v == 8) {
                    $h = 8;
                } else if ($v == 9) {
                    $i = 9;
                } else if ($v == 10) {
                    $j = 10;
                } else if ($v == 11) {
                    $ka = 11;
                } else if ($v == 12) {
                    $l = 12;
                } else if ($v == 13) {
                    $m = 13;
                } else if ($v == 14) {
                    $n = 14;
                } else if ($v == 15) {
                    $o = 15;
                } else if ($v == 16) {
                    $p = 16;
                } else if ($v == 17) {
                    $q = 17;
                } else if ($v == 18) {
                    $r = 18;
                } else if ($v == 19) {
                    $s = 19;
                } else if ($v == 20) {
                    $t = 20;
                } else if ($v == 21) {
                    $u = 21;
                } else if ($v == 22) {
                    $va = 22;
                } else if ($v == 23) {
                    $w = 23;
                } else if ($v == 24) {
                    $x = 24;
                } else if ($v == 25) {
                    $y = 25;
                } else if ($v == 26) {
                    $z = 26;
                }
            }
        }
        // print_r($i);die;
        $this->assign('a', $a);
        $this->assign('b', $b);
        $this->assign('c', $c);
        $this->assign('d', $d);
        $this->assign('e', $e);
        $this->assign('f', $f);
        $this->assign('g', $g);
        $this->assign('h', $h);
        $this->assign('ia', $i);
        $this->assign('j', $j);
        $this->assign('k', $ka);
        $this->assign('l', $l);
        $this->assign('m', $m);
        $this->assign('n', $n);
        $this->assign('o', $o);
        $this->assign('p', $p);
        $this->assign('q', $q);
        $this->assign('r', $r);
        $this->assign('s', $s);
        $this->assign('t', $t);
        $this->assign('u', $u);
        $this->assign('v', $va);
        $this->assign('w', $w);
        $this->assign('x', $x);
        $this->assign('y', $y);
        $this->assign('z', $z);
        $this->assign('info', $info);

        $this->assign('floor_id',$floor_id);
        $this->assign('cur','权限管理');
        $this->display();
    }
    //权限编辑
    // public function role_edit(){
    //     $this->assign('cur','权限管理');
    //     $this->display();
    // }
    //权限管理
    public function role_manage(){
        $id=session('me_id');
        $floor_id=session('floor_id');
        $map['floor_id']=$floor_id;
        $_REQUEST['role_name']?$map['role_name']=array('like',"%$_REQUEST[role_name]%"):'';
        $this->assign('role_name',$_REQUEST['role_name']);
        //现在我是照着权限根据楼盘来弄 可能是跟机构
        $role_list=M('jurisdiction')->order('id desc')->where($map)->select();

        foreach($role_list as $k=>$v){
            $role_list[$k]['create_time']=date('Y-m-d',$v['create_time']);
            if($v['status']==1){
                $role_list[$k]['state']='启用';
            }else{
                $role_list[$k]['state']='禁用';
            }
        }
        // print_r($role_list);die;
        $this->assign('list',$role_list);
        $this->assign('cur','权限管理');
        $this->display();
    }

    //删除权限
    public function role_del(){
        $id=$_REQUEST['id'];
        $res=M('jurisdiction')->where("id=$id")->delete();
        if($res){
            $this->success('成功',U('Mechanism/role_manage'));
        }else{
            $this->error('失败',U('Mechanism/role_manage'));
        }
    }
    //打卡记录
    public function punch_record(){
        $this->assign('cur','打卡记录');
        $this->display();
    }
    //楼盘管理
    public function floor_index(){
        $id=session('me_id');
        //找出这个机构下面的楼盘
        $map['me_id']=$id;
        $map['is_del']=array('neq',1);

        $_REQUEST['name']?$map['name']=array('like',"%$_REQUEST[name]%"):'';
        $this->assign('name',$_REQUEST['name']);

        $_REQUEST['status']?$map['status']=array('like',"%$_REQUEST[status]%"):'';
        $this->assign('status',$_REQUEST['status']);

        $count = M('department_staff')->where($map)->count();
        $Page = new \Think\Page($count, 10,$_REQUEST);// 实例化分页类 传入总记录数和每页显示的记录数(25)

        $list=M('floor')->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->where($map)->select();

        foreach($list as $k=>$v){
            $list[$k]['img']=M('picture')->where("id=$v[img]")->getfield('path');
            $list[$k]['create_time']=date('Y-m-d',$v['create_time']);
            if($v['status']==1){
                $list[$k]['status']='待售';
            }elseif($v['status']==2){
                $list[$k]['status']='在售';
            }else{
                $list[$k]['status']='售罄';
            }
        }
        // print_r($list);die;
        $show=$Page->show();
        $this->assign('_page',$show);
        $this->assign('cur','楼盘管理');
        $this->assign('list',$list);
        $this->display();
    }
    //新增楼盘
    public function floor_add(){
        $id=session('me_id');
        if($_POST){
            if($_POST['menu']==1){
                $data['name']=$_POST['name'];
                $data['hot_line']=$_POST['hot_line'];
                $data['min_area']=$_POST['min_area'];
                $data['max_area']=$_POST['max_area'];
                $data['status']=$_POST['status'];
                $data['price']=$_POST['price'];

                $checked=$_POST['checked'];
                foreach($checked as $k=>$v){
                    $selling.=','.$v;
                }
                $data['selling_point']=substr($selling, 1);

                $data['img']=$_POST['img'];

                $imgs1=$_POST['imgs1'];
                $imgs2=$_POST['imgs2'];
                $imgs3=$_POST['imgs3'];

                $data['imgs']=$imgs1.','.$imgs2.','.$imgs3;
                $data['project_poster']=$_POST['project_poster'];
                $data['video']=$_POST['video'];
                $data['me_id']=session('me_id');
                $data['create_time']=time();
                $res=M('floor')->add($data);
                if($res){
                    return api_return(1,'成功',$res);
                }else{
                    return api_return(2,'失败');
                }
            }elseif($_POST['menu']==2){
                $save_data['id']=$_POST['floor_id'];
                $save_data['sand_img']=$_POST['sand_img'];
                $res=M('floor')->save($save_data);
                if($res){
                    return api_return(1,'成功');
                }else{
                    return api_return(2,'失败');
                }
            }elseif($_POST['menu']==3){

            }elseif($_POST['menu']==4){
                $save_data['id']=$_POST['floor_id'];

                $map=explode(',',$_POST['map']);
                $save_data['lng']=$map[0];
                $save_data['lat']=$map[1];

                $ak='jtgUnOglQDkFEpRFD2qhnb3umKGQIL15';
                $lat=$map[1];
                $lng=$map[0];
                $address=$this->getCityName($ak,$lat,$lng);

                $save_data['province']=$address['province'];
                $save_data['city']=$address['city'];
                $save_data['area']=$address['area'];
                $save_data['address']=$address['address'];
                // print_r($save_data);die;
                $res=M('floor')->save($save_data);
                if($res){
                    return api_return(1,'成功');
                }else{
                    return api_return(2,'失败');
                }
            }

        }
        $this->assign('menu',$menu);
        $this->assign('cur','楼盘管理');
        $this->display();
    }

    public function floor_del(){
        $id=I('id');
        $res=M('floor')->where("id=$id")->save(['is_del'=>1]);
        if($res){
            return $this->success('成功',U('floor_index'));
        }else{
            return $this->error('失败',U('floor_index'));
        }
    }

    //上线 下线
    public function floor_up(){
        $data['id']=I('id');
        $data['state']=I('is_up');
        $res=M('floor')->save($data);
        if($res){
            return $this->success('成功',U('Mechanism/floor_index'));
        }else{
            return $this->error('失败',U('Mechanism/floor_index'));
        }
    }
    public function getCityName($ak,$lat,$lng)
    {
        // $ak='jtgUnOglQDkFEpRFD2qhnb3umKGQIL15';
        // $lat='28.22778';
        // $lng='112.93886';
        // $callback=array('cityCode'=>0,'cityName'=>'');
        // $apiUrl='http://api.map.baidu.com/geocoder/v2/?location='.$lat.','.$lng.'&output=json&coordtype=wgs84ll&pois=1&ak='.$ak;


        $apiUrl='https://api.map.baidu.com/reverse_geocoding/v3/?ak='.$ak.'&location='.$lat.','.$lng.'&output=json&pois=1';

        $ret=json_decode(file_get_contents($apiUrl),true);
        // print_r($ret);
        if($ret['status']==0){
 
            // $callback['cityCode']=$ret['result']['cityCode'];
            // $callback['cityName']=$ret['result']['addressComponent']['city'];
            $address['address']=$ret['result']['formatted_address'];
            $address['province']=$ret['result']['addressComponent']['province'];
            $address['city']=$ret['result']['addressComponent']['city'];
            $address['area']=$ret['result']['addressComponent']['district'];
        }

        return $address;
    }

    public function getCityName1($ak,$lat,$lng)
    {
        // $ak='jtgUnOglQDkFEpRFD2qhnb3umKGQIL15';
        // $lat='28.22778';
        // $lng='112.93886';
        // $callback=array('cityCode'=>0,'cityName'=>'');
        // $apiUrl='http://api.map.baidu.com/geocoder/v2/?location='.$lat.','.$lng.'&output=json&coordtype=wgs84ll&pois=1&ak='.$ak;


        $apiUrl='https://api.map.baidu.com/reverse_geocoding/v3/?ak='.$ak.'&location='.$lat.','.$lng.'&output=json&pois=1';

        $ret=json_decode(file_get_contents($apiUrl),true);
        // print_r($ret);
        if($ret['status']==0){
 
            // $callback['cityCode']=$ret['result']['cityCode'];
            // $callback['cityName']=$ret['result']['addressComponent']['city'];
            $address['address']=$ret['result']['formatted_address'];
            $address['province']=$ret['result']['addressComponent']['province'];
            $address['city']=$ret['result']['addressComponent']['city'];
            $address['area']=$ret['result']['addressComponent']['district'];
        }

        return api_return(1,'成功',$address);
    }

    //编辑楼盘
    public function floor_edit(){
        if($_POST){
            if($_POST['menu']==1){
                $data['id']=$_POST['floor_id'];
                $data['name']=$_POST['name'];
                $data['hot_line']=$_POST['hot_line'];
                $data['min_area']=$_POST['min_area'];
                $data['max_area']=$_POST['max_area'];
                $data['status']=$_POST['status'];
                $data['price']=$_POST['price'];

                $checked=$_POST['checked'];
                foreach($checked as $k=>$v){
                    $selling.=','.$v;
                }
                $data['selling_point']=substr($selling, 1);

                $data['img']=$_POST['img'];

                $imgs1=$_POST['imgs1'];
                $imgs2=$_POST['imgs2'];
                $imgs3=$_POST['imgs3'];

                $data['imgs']=$imgs1.','.$imgs2.','.$imgs3;
                $data['project_poster']=$_POST['project_poster'];
                $data['video']=$_POST['video'];
                $data['me_id']=session('me_id');
                $res=M('floor')->save($data);
                if($res){
                    return api_return(1,'成功',$res);
                }else{
                    return api_return(2,'失败');
                }
            }elseif($_POST['menu']==2){
                $save_data['id']=$_POST['floor_id'];
                $save_data['sand_img']=$_POST['sand_img'];
                $res=M('floor')->save($save_data);
                if($res){
                    return api_return(1,'成功');
                }else{
                    return api_return(2,'失败');
                }
            }elseif($_POST['menu']==3){

            }elseif($_POST['menu']==4){
                $save_data['id']=$_POST['floor_id'];

                $map=explode(',',$_POST['map']);
                $save_data['lng']=$map[0];
                $save_data['lat']=$map[1];

                $ak='jtgUnOglQDkFEpRFD2qhnb3umKGQIL15';
                $lat=$map[1];
                $lng=$map[0];
                $address=$this->getCityName($ak,$lat,$lng);

                $save_data['province']=$address['province'];
                $save_data['city']=$address['city'];
                $save_data['area']=$address['area'];
                $save_data['address']=$address['address'];
                // print_r($save_data);die;
                $res=M('floor')->save($save_data);
                if($res){
                    return api_return(1,'成功');
                }else{
                    return api_return(2,'失败');
                }
            }
        }
        $id=I('id');
        $info=M('floor')->where("id=$id")->find();

        $type_list=M('house_type')->where("floor_id=$id")->select();
        foreach($type_list as $k=>$v){
            if($v['house_img']){
                $type_list[$k]['house_img']=M('picture')->where("id=$v[house_img]")->getfield('path');
            }
            $type_list[$k]['floor_name']=M('floor')->where("id=$v[floor_id]")->getfield('name');
        }
        $this->assign('type_list',$type_list);
        $build_list=M('building')->where("floor_id=$id")->select();
        foreach($build_list as $k=>$v){
            //总共多少房源
            $build_list[$k]['count_fy']=M('generate_room')->where("build_id=$v[id]")->count();
            //在售房源
            $build_list[$k]['count_zs']=M('generate_room')->where("build_id=$v[id] && status=2")->count();

            $build_list[$k]['floor_name']=M('floor')->where("id=$v[floor_id]")->getfield('name');
        }
        $this->assign('build_list',$build_list);
        if($info['img']){
            $info['img_src']=M('picture')->where("id=$info[img]")->getfield('path');
        }
        if($info['imgs']){
            $imgs=explode(',',$info['imgs']);
            foreach($imgs as $k=>$v){
                $info['imgg_src'][$k]=M('picture')->where("id=$v")->getfield('path');
            }
            $this->assign('imgs',$imgs);
        }
        if($info['project_poster']){
            $info['project_poster_src']=M('picture')->where("id=$info[project_poster]")->getfield('path');
        }
        if($info['sand_img']){
            $info['sand_img_src']=M('picture')->where("id=$info[sand_img]")->getfield('path');
        }
        if(!empty($info['lng'])&& !empty($info['lat'])){
        $info['map']=$info['lng'].",".$info['lat'].",15";
        }
        $a=strstr($info['selling_point'],'绿化率高');
        $b=strstr($info['selling_point'],'车位充足');
        $c=strstr($info['selling_point'],'现房');
        $d=strstr($info['selling_point'],'学区房');
        $e=strstr($info['selling_point'],'近地铁');
        $f=strstr($info['selling_point'],'花园洋房');
        $g=strstr($info['selling_point'],'小户型');
        $h=strstr($info['selling_point'],'低密度');
        $i=strstr($info['selling_point'],'复式');
        $j=strstr($info['selling_point'],'品牌地产');
        $this->assign('a',$a);
        $this->assign('b',$b);
        $this->assign('c',$c);
        $this->assign('d',$d);
        $this->assign('e',$e);
        $this->assign('f',$f);
        $this->assign('g',$g);
        $this->assign('h',$h);
        $this->assign('i',$i);
        $this->assign('j',$j);
        $this->assign('info',$info);
        $this->assign('cur','楼盘管理');
        $this->display();
    }
    //户型管理
    public function house_type(){
        $id=session('me_id');
        // $floor_id=$_SESSION['onethink_home']['me_info']['floor_id'];

        $map['a.me_id']=session('me_id');
        $_REQUEST['floor_name']?$map['b.name']=array('like',"%$_REQUEST[floor_name]%"):'';
        $this->assign('floor_name',$_REQUEST['floor_name']);
        $count = M('house_type a')->join('left join l_floor b on a.floor_id=b.id')->where($map)->count();
        $Page = new \Think\Page($count, 10,$_REQUEST);// 实例化分页类 传入总记录数和每页显示的记录数(25)


        $list=M('house_type a')->join('left join l_floor b on a.floor_id=b.id')->limit($Page->firstRow . ',' . $Page->listRows)->where($map)->order('id desc')->field('a.*')->select();
        foreach($list as $k=>$v){
            if($v['house_img']){
                $list[$k]['house_img']=M('picture')->where("id=$v[house_img]")->getfield('path');
            }
            $list[$k]['floor_name']=M('floor')->where("id=$v[floor_id]")->getfield('name');
        }
        $show=$Page->show();
        $this->assign('_page',$show);

        $this->assign('cur','户型管理');
        $this->assign('list',$list);
        $this->display();
    }
    //新增户型
    public function house_add(){
        $me_id=session('me_id');


        if($_POST && empty($_POST['id'])){
            $data['name']=$_POST['name'];
            $data['floor_id']=$_POST['floor_id'];
            $data['status']=$_POST['status'];
            $data['house_config']=$_POST['house_config'];
            $data['house_area']=$_POST['house_area'];
            $data['house_img']=$_POST['house_img'];
            $data['price']=$_POST['price'];
            $data['me_id']=$me_id;
            $data['create_time']=time();
            $res=M('house_type')->add($data);
            if($res){
                return $this->success('成功',U('Mechanism/house_type'));
            }else{
                return $this->error('失败',U('Mechanism/house_type'));
            }
        }elseif($_POST['id']){
            $data['name']=$_POST['name'];
            $data['floor_id']=$_POST['floor_id'];
            $data['status']=$_POST['status'];
            $data['house_config']=$_POST['house_config'];
            $data['house_area']=$_POST['house_area'];
            $data['house_img']=$_POST['house_img'];
            $data['me_id']=session('me_id');
            $data['price']=$_POST['price'];
            $data['id']=$_POST['id'];
            $res=M('house_type')->save($data);
            if($res){
                return $this->success('成功',U('Mechanism/house_type'));
            }else{
                return $this->error('失败',U('Mechanism/house_type'));
            }
        }
        $hid=I('id');
        if($hid){
            //编辑
            $info=M('house_type')->where("id=$hid")->find();
            if($info['house_img']){
                $info['img']=M('picture')->where("id=$info[house_img]")->getfield('path');
            }else{
                $info['img']='';
            }
        }
        $floor_list=M('floor')->where("me_id=$me_id")->select();
        $this->assign('floor_list',$floor_list);
        $this->assign('cur','户型管理');
        $this->assign('info',$info);
        $this->display();
    }
    //删除户型
    public function house_del(){
        $id=I('id');
        $res=M('house_type')->where("id=$id")->delete();
        if($res){
            return $this->success('成功',U('house_type'));
        }else{
            return $this->error('失败',U('house_type'));
        }
    }
    //楼栋管理
    public function build_manage(){
        $id=session('me_id');
        // $floor_id=$_SESSION['onethink_home']['me_info']['floor_id'];
        $map['me_id']=$id;
        $count=M('building')->where($map)->count();
        $Page = new \Think\Page($count, 10,$_REQUEST);
        $list=M('building')->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->where($map)->select();
        foreach($list as $k=>$v){
            $list[$k]['floor_name']=M('floor')->where("id=$v[floor_id]")->getfield('name');
            $re=M('generate_room')->where("build_id=$v[id]")->find();
            if($re){
                $list[$k]['is_sc']=1;
            }else{
                $list[$k]['is_sc']=2;
            }
        }
        // print_r($list);die;
        $show=$Page->show();
        $this->assign('_page',$show);
        $this->assign('cur','楼栋管理');
        $this->assign('list',$list);
        $this->display();
    }
    //新增楼栋(弃用)
    public function build_add123(){
        $me_id=session('me_id');
        // $floor_id=$_SESSION['onethink_home']['me_info']['floor_id'];
        if(!$me_id){
            return $this->error('请登录',U('login'));
        }
        if($_POST && empty($_POST['id'])){
            $data['floor_id']=$_POST['floor_id'];
            $data['status']=$_POST['status'];
            $data['building_name']=$_POST['building_name'];
            //楼盘名称跟单元数 少了一个 因为有问题
            $data['elevator']=$_POST['elevator'];
            $data['type']=$_POST['type'];
            $data['layer']=$_POST['layer'];
            $data['households']=$_POST['households'];
            $data['house_sale']=$_POST['house_sale'];
            $data['sale_type']=$_POST['sale_type'];
            $data['img']=$_POST['img'];
            $data['me_id']=session('me_id');
            $res=M('building')->add($data);
            if($res){
                $this->success('成功',U('Mechanism/build_manage'));
            }else{
                $this->error('失败',U('Mechanism/build_manage'));
            }
        }elseif($_POST['id']){
            $data['floor_id']=$_POST['floor_id'];
            $data['status']=$_POST['status'];
            $data['building_name']=$_POST['building_name'];
            //楼盘名称跟单元数 少了一个 因为有问题
            $data['elevator']=$_POST['elevator'];
            $data['type']=$_POST['type'];
            $data['layer']=$_POST['layer'];
            $data['households']=$_POST['households'];
            $data['house_sale']=$_POST['house_sale'];
            $data['sale_type']=$_POST['sale_type'];
            $data['img']=$_POST['img'];
            $data['me_id']=session('me_id');
            $data['id']=$_POST['id'];
            $res=M('building')->save($data);
            if($res){
                $this->success('成功',U('Mechanism/build_manage'));
            }else{
                $this->error('失败',U('Mechanism/build_manage'));
            }
        }
        $hid=I('id');
        if($hid){
            //编辑
            $info=M('building')->where("id=$hid")->find();
        }
        $floor_list=M('floor')->where("me_id=$me_id")->select();

        $ff_id=M('floor')->where("me_id=$me_id")->order('id asc')->getfield('id');
        // print_r($ff_id);die;
        //第一个楼盘下有多少户型
        $type_list=M('house_type')->where("floor_id=$ff_id")->select();
        foreach($type_list as $k=>$v){
            if($v['status']==1){
                $type_list[$k]['status_name']='待售';
            }elseif($v['status']==2){
                $type_list[$k]['status_name']='在售';
            }elseif($v['status']==3){
                $type_list[$k]['status_name']='售罄';
            }
            
        }
        // print_r($type_list);die;
        $this->assign('type_list',$type_list);
        $this->assign('floor_list',$floor_list);
        $this->assign('cur','楼栋管理');
        $this->assign('info',$info);
        $this->display();
    }


    //新增楼栋
    public function build_add(){
        $me_id=session('me_id');
        $floor_list=M('floor')->where("me_id=$me_id")->select();

        $ff_id=M('floor')->where("me_id=$me_id")->order('id asc')->getfield('id');
        // print_r($ff_id);die;
        //第一个楼盘下有多少户型
        $type_list=M('house_type')->where("floor_id=$ff_id")->select();
        foreach($type_list as $k=>$v){
            if($v['status']==1){
                $type_list[$k]['status_name']='待售';
            }elseif($v['status']==2){
                $type_list[$k]['status_name']='在售';
            }elseif($v['status']==3){
                $type_list[$k]['status_name']='售罄';
            }
            
        }
        $this->assign('type_list',$type_list);
        $this->assign('floor_list',$floor_list);
        $this->assign('cur','楼栋管理');
        $this->display();
    }


    //编辑楼栋
    public function build_edit(){
        $me_id=session('me_id');
        $floor_list=M('floor')->where("me_id=$me_id")->select();

        $ff_id=M('floor')->where("me_id=$me_id")->order('id asc')->getfield('id');
        // print_r($ff_id);die;
        //第一个楼盘下有多少户型
        $type_list=M('house_type')->where("floor_id=$ff_id")->select();
        foreach($type_list as $k=>$v){
            if($v['status']==1){
                $type_list[$k]['status_name']='待售';
            }elseif($v['status']==2){
                $type_list[$k]['status_name']='在售';
            }elseif($v['status']==3){
                $type_list[$k]['status_name']='售罄';
            }
            
        }
        $id=I('id');
        $info=M('building')->where("id=$id")->find();
        $list=explode(',', $info['sale_type']);
        foreach($list as $k=>$v){
            $list[$k]=M('house_type')->where("id=$v")->find();
            if($list[$k]['status']==1){
                $list[$k][$k]['status_name']='待售';
            }elseif($list[$k]['status']==2){
                $list[$k][$k]['status_name']='在售';
            }elseif($list[$k]['status']==3){
                $list[$k][$k]['status_name']='售罄';
            }
        }
        $this->assign('list',$list);
        $this->assign('info',$info);
        $this->assign('type_list',$type_list);
        $this->assign('floor_list',$floor_list);
        $this->assign('cur','楼栋管理');
        $this->display();
    }

    //ajxj 一部请求 户型  根据选择的楼盘来显示对应的户型
    public function house_show(){
        $floor_id=$_POST['floor_id'];
        $type_list=M('house_type')->where("floor_id=$floor_id")->select();
        foreach($type_list as $k=>$v){
        if($v['status']==1){
            $type_list[$k]['status_name']='待售';
        }elseif($v['status']==2){
            $type_list[$k]['status_name']='在售';
        }elseif($v['status']==3){
            $type_list[$k]['status_name']='售罄';
        }
        
    }
        // printer_abort($type_list);die;
        return api_return(1, '信息获取成功', $type_list);
    }
    //确认选择 选择了哪些户型，显示在页面上
    public function choice_type(){
        $chk_value=$_POST['chk_value'];
        foreach($chk_value as $k=>$v){
            $data[$k]=M('house_type')->where("id=$v")->find();
            if($data[$k]['status']==1){
                $data[$k]['status_name']='待售';
            }elseif($data[$k]['status']==2){
                $data[$k]['status_name']='在售';
            }elseif($data[$k]['status']==3){
                $data[$k]['status_name']='售罄';
            }
        }

        return api_return(1,'成功',$data);
    }
    //保存新增户型
    public function save_type(){
        // print_r($_POST);die;
        if($_POST['id']){
            $data['id']=$_POST['id'];
            $data['floor_id']=$_POST['floor_id'];
            $data['status']=$_POST['status'];
            $data['building_name']=$_POST['building_name'];
            //楼盘名称跟单元数 少了一个 因为有问题
            $data['elevator']=$_POST['elevator'];
            $data['type']=$_POST['type'];
            $data['layer']=$_POST['layer'];
            $data['households']=$_POST['households'];
            $data['house_sale']=$_POST['house_sale'];
            $data['sale_type']=$_POST['sale_type'];
            $data['unit_number']=$_POST['unit_number'];
            $data['img']=$_POST['img'];
            $data['me_id']=session('me_id');
            $chk=$_POST['chk'];
            foreach($chk as $k=>$v){
                if($k==0){
                    $gg=$v;
                }else{
                    $gg.=','.$v;
                }

            }
            $data['sale_type']=$gg;
            $res=M('building')->save($data);
        }else{
            $data['floor_id']=$_POST['floor_id'];
            $data['status']=$_POST['status'];
            $data['building_name']=$_POST['building_name'];
            //楼盘名称跟单元数 少了一个 因为有问题
            $data['elevator']=$_POST['elevator'];
            $data['type']=$_POST['type'];
            $data['layer']=$_POST['layer'];
            $data['households']=$_POST['households'];
            $data['house_sale']=$_POST['house_sale'];
            $data['sale_type']=$_POST['sale_type'];
            $data['unit_number']=$_POST['unit_number'];
            $data['img']=$_POST['img'];
            $data['me_id']=session('me_id');
            $chk=$_POST['chk'];
            foreach($chk as $k=>$v){
                if($k==0){
                    $gg=$v;
                }else{
                    $gg.=','.$v;
                }

            }
            $data['sale_type']=$gg;
            $res=M('building')->add($data);
        }

        if($res){
            return api_return(1,'成功');
        }else{
            return api_return(2,'失败');
        }
    }

    //生成房间
    public function generate_room(){
        $id=I('id');
        $info=M('building')->where("id=$id")->find();
        $number=$info['unit_number'];
        $list=array();
        for($i=0;$i<$number;$i++){
            $list[$i]['id']=$i+1;
            $list[$i]['layer']=$info['layer'];
        }
        // print_r($list);die;
        $list1=json_encode($list);
        $this->assign('list1',$list1);
        $this->assign('list',$list);
        $this->assign('floor_id',$info['floor_id']);
        $this->assign('id',$id);
        $this->assign('cur','楼栋管理');
        $this->display();
    }

    //生成房间保存到数据库
    public function save_room(){
        $add_data['me_id']=session('me_id');
        $add_data['build_id']=$_POST['build_id'];
        $add_data['floor_id']=$_POST['floor_id'];
        $chk=$_POST['chk'];//结束房号
        $chk1=$_POST['chk1'];//单元号
        $chk2=$_POST['chk2'];//单元总楼层
        //1个楼栋有4个单元，每个单元有10楼，每楼有4户
        $cc=0;
        for($b=1;$b<=$chk1;$b++){
            //多少单元循环多少次
            $add_data['unit']=$b;

            $kk=$chk2[$cc];
            for($a=1;$a<=$kk;$a++){
                //多少层循环多少次
                $add_data['hierarchy']=$a;
                $yy=$chk[$cc];
                for($i=1;$i<=$yy;$i++){
                    //多少个房间号循环多少次
                    $add_data['room_id']=$a.'0'.$i;
                    $add_data['create_time']=time();
                    // print_r($add_data);
                    $res=M('generate_room')->add($add_data);
                }
            }
            $cc++;
        }
        if($res){
            return api_return(1,'成功');
        }else{
            return api_return(2,'失败');
        }

    }
    //房源管理
    public function souse_res(){
        $id=session('me_id');
        $floor_id=$_SESSION['onethink_home']['me_info']['floor_id'];

        $map['me_id']=$id;
        $_REQUEST['build_id']?$map['build_id']=$_REQUEST['build_id']:'';
        $this->assign('build_id',$_REQUEST['build_id']);
        $count = M('generate_room')->where($map)->count();
        $Page = new \Think\Page($count, 10,$map);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $list=M('generate_room')->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->where($map)->select();
        foreach($list as $k=>$v){
            $list[$k]['floor_name']=M('floor')->where("id=$v[floor_id]")->getfield('name');
            if($v['status']==1){
                $list[$k]['status_name']='待售';
            }elseif($v['status']==2){
                $list[$k]['status_name']='在售';
            }elseif($v['status']==3){
                $list[$k]['status_name']='已售';
            }elseif($v['status']==4){
                $list[$k]['status_name']='预留';
            }elseif($v['status']==5){
                $list[$k]['status_name']='认筹';
            }elseif($v['status']==6){
                $list[$k]['status_name']='认购';
            }elseif($v['status']==7){
                $list[$k]['status_name']='签约';
            }
        }

        $show=$Page->show();
        $this->assign('_page',$show);

        $this->assign('cur','房源管理');
        $this->assign('list',$list);
        $this->display();
    }
    //新增 编辑房源
    // public function res_add(){
    //     $id=$_SESSION['onethink_home']['me_info']['id'];
    //     $floor_id=$_SESSION['onethink_home']['me_info']['floor_id'];

    //     if($_POST && empty($_POST['id'])){
    //         $data['floor_id']=$_POST['floor_id'];
    //         $data['build_id']=$_POST['build_id'];
    //         $data['unit']=$_POST['unit'];
    //         $data['room_number']=$_POST['room_number'];
    //         $data['floor_number']=$_POST['floor_number'];
    //         $data['structure']=$_POST['structure'];
    //         $data['orientation']=$_POST['orientation'];
    //         $data['built_area']=$_POST['built_area'];
    //         $data['unit_price']=$_POST['unit_price'];
    //         $data['price_s']=$_POST['price_s'];
    //         $data['book_price']=$_POST['book_price'];
    //         $data['total_price']=$_POST['total_price'];
    //         $data['me_id']=session('me_id');
    //         $res=M('housing_resources')->add($data);
    //         if($res){
    //             $this->success('成功',U('Mechanism/souse_res'));
    //         }else{
    //             $this->error('失败',U('Mechanism/souse_res'));
    //         }
    //     }elseif($_POST['id']){
    //         $data['floor_id']=$_POST['floor_id'];
    //         $data['build_id']=$_POST['build_id'];
    //         $data['unit']=$_POST['unit'];
    //         $data['room_number']=$_POST['room_number'];
    //         $data['floor_number']=$_POST['floor_number'];
    //         $data['structure']=$_POST['structure'];
    //         $data['orientation']=$_POST['orientation'];
    //         $data['built_area']=$_POST['built_area'];
    //         $data['unit_price']=$_POST['unit_price'];
    //         $data['price_s']=$_POST['price_s'];
    //         $data['book_price']=$_POST['book_price'];
    //         $data['total_price']=$_POST['total_price'];
    //         $data['me_id']=session('me_id');
    //         $data['id']=$_POST['id'];
    //         $res=M('housing_resources')->save($data);
    //         if($res){
    //             $this->success('成功',U('Mechanism/souse_res'));
    //         }else{
    //             $this->error('失败',U('Mechanism/souse_res'));
    //         }
    //     }
    //     $hid=I('id');
    //     if($hid){
    //         //编辑
    //         $info=M('housing_resources')->where("id=$hid")->find();
    //     }
    //     $this->assign('cur','房源管理');
    //     $this->assign('info',$info);
    //     $this->display();
    // }

    //编辑房源
    public function res_edit(){
        if($_POST){
            $data=$_POST;
            $res=M('generate_room')->save($data);
            if($res){
                return $this->success('成功',U('souse_res'));
            }else{
                return $this->error('失败',U('souse_res'));
            }
        }


        $hid=I('id');
        //编辑
        $info=M('generate_room')->where("id=$hid")->find();
        $info['floor_name']=M('floor')->where("id=$info[floor_id]")->getfield('name');

        //户型
        // $type_list=M('house_type')->where("build_id=$info[build_id]")->select();
        $sale_type=M('building')->where("id=$info[build_id]")->getfield('sale_type');
        if($sale_type){
            $list=explode(',', $sale_type);
            $gg=array();
            foreach($list as $k=>$v){
                $gg[$k]['id']=$v;
                $gg[$k]['name']=M('house_type')->where("id=$v")->getfield('name');
            }
        }
        // print_r($gg);die;
        $this->assign('list',$gg);
        $this->assign('type_list',$type_list);
        $this->assign('cur','房源管理');
        $this->assign('info',$info);
        $this->display();
    }
    //新闻列表(促销活动)
    public function news_index(){
        $id=session('me_id');
        // $floor_id=$_SESSION['onethink_home']['me_info']['floor_id'];
        $map['me_id']=session('me_id');
        $_REQUEST['title']?$map['title']=array('like',"%$_REQUEST[title]%"):'';
        $this->assign('title',$_REQUEST['title']);
        $count = M('news')->where($map)->count();
        $Page = new \Think\Page($count, 10,$_REQUEST);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $list=M('news')->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->where($map)->select();
        foreach($list as $k=>$v){
            $list[$k]['class_name']=M('class')->where("id=$v[class_id]")->getfield('name');
            $list[$k]['create_time']=date('Y-m-d',$v['create_time']);
        }
        $show=$Page->show();
        // print_r($id);die;

        $this->assign('cur','新闻管理');
        $this->assign('_page',$show);
        $this->assign('list',$list);
        $this->display();
    }
    //发布新闻
    public function news_add(){
        $id=session('me_id');
        // $floor_id=$_SESSION['onethink_home']['me_info']['floor_id'];

        if($_POST && empty($_POST['id'])){
            $data['title']=$_POST['title'];
            $data['source']=$_POST['source'];
            $data['class_id']=$_POST['class_id'];//页面没有分类，但是逻辑上是要有
            $data['sort']=$_POST['sort'];
            $data['content']=$_POST['content'];
            $data['me_id']=session('me_id');
            $data['create_time']=time();
            $res=M('news')->add($data);
            if($res){
                return $this->success('成功',U('Mechanism/news_index'));
            }else{
                return $this->error('失败',U('Mechanism/news_index'));
            }
        }elseif($_POST['id']){
            $data['title']=$_POST['title'];
            $data['source']=$_POST['source'];
            $data['class_id']=$_POST['class_id'];//页面没有分类，但是逻辑上是要有
            $data['sort']=$_POST['sort'];
            $data['content']=$_POST['content'];
            $data['me_id']=session('me_id');
            $data['id']=$_POST['id'];
            $res=M('news')->save($data);
            if($res){
                return $this->success('成功',U('Mechanism/news_index'));
            }else{
                return $this->error('失败',U('Mechanism/news_index'));
            }
        }
        $hid=I('id');
        if($hid){
            //编辑
            $info=M('news')->where("id=$hid")->find();
        }
        $type_list=M('class')->where("me_id=$id")->select();
        // print_r($type_list);die;
        $this->assign('type_list',$type_list);
        $this->assign('cur','新闻管理');
        $this->assign('info',$info);
        $this->display(); 
    }
    //删除新闻
    public function news_del(){
        $id=I('id');
        $res=M('news')->where("id=$id")->delete();
        if($res){
            return $this->success('成功',U('Mechanism/news_index'));
        }else{
            return $this->error('失败',U('Mechanism/news_index'));
        }
    }

    //推荐新闻  取消推荐
    public function recommend(){
        $data['id']=I('id');
        $data['recommend']=I('recommend');
        $res=M('news')->save($data);
        if($res){
            return $this->success('成功',U('Mechanism/news_index'));
        }else{
            return $this->error('失败',U('Mechanism/news_index'));
        }
    }

    //新闻分类
    public function class_index(){
        $id=session('me_id');
        // $floor_id=$_SESSION['onethink_home']['me_info']['floor_id'];

        $map['me_id']=session('me_id');
        $_REQUEST['name1']?$map['name']=array('like',"%$_REQUEST[name1]%"):'';
        $this->assign('name1',$_REQUEST['name1']);
        $count = M('class')->where($map)->count();
        $Page = new \Think\Page($count, 10,$_REQUEST);// 实例化分页类 传入总记录数和每页显示的记录数(25)

        $list=M('class')->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->where($map)->select();
        foreach($list as $k=>$v){
            $list[$k]['create_time']=date('Y-m-d H:i:s',$v['create_time']);
        }
        $show=$Page->show();
        $this->assign('cur','新闻分类');
        $this->assign('_page',$show);
        $this->assign('list',$list);
        $this->display();
    }
    //添加 编辑分类
    public function class_add(){
        $id=$_SESSION['onethink_home']['me_info']['id'];
        $floor_id=$_SESSION['onethink_home']['me_info']['floor_id'];

        if($_POST && empty($_POST['id'])){
            $data['name']=$_POST['name'];
            $data['me_id']=session('me_id');
            $data['create_time']=time();
            $res=M('class')->add($data);
            if($res){
                $this->success('成功',U('Mechanism/class_index'));
            }else{
                $this->error('失败',U('Mechanism/class_index'));
            }
        }elseif($_POST['id']){
            $data['name']=$_POST['name'];
            $data['me_id']=session('me_id');
            $data['id']=$_POST['id'];
            $res=M('class')->save($data);
            if($res){
                $this->success('成功',U('Mechanism/class_index'));
            }else{
                $this->error('失败',U('Mechanism/class_index'));
            }
        }
        $hid=I('id');
        if($hid){
            $info=M('class')->where("id=$hid")->find();
        }
        $this->assign('cur','新闻分类');
        $this->assign('info',$info);
        $this->display(); 
    }
    public function class_name(){
        $id=I('id');
        $name=M('class')->where("id=$id")->getfield('name');
        return api_return(1,'成功',$name);
    }
    public function class_aname(){
        $name=I('name');
        $id=I('id');
        if($id){
            $res=M('class')->where("id=$id")->save(['name'=>$name]);
        }else{
            $data['create_time']=time();
            $data['name']=I('name');
            $data['me_id']=session('me_id');
            $res=M('class')->add($data);
        }
        if($res){
            return api_return(1,'成功');
        }else{
            return api_return(2,'失败');
        }
        
    }
    public function class_del(){
        $id=I('id');
        $res=M('class')->where("id=$id")->delete();
        if($res){
            return $this->success('成功',U('class_index'));
        }else{
            return $this->error('失败',U('class_index'));
        }
    }
    //活动管理
    public function activity_index(){
        // $id=session('me_id');
        // $floor_id=$_SESSION['onethink_home']['me_info']['floor_id'];
        $map['me_id']=session('me_id');
        $_REQUEST['title']?$map['title']=array('like',"%$_REQUEST[title]%"):'';
        $this->assign('title',$_REQUEST['title']);
        $count = M('activity')->where($map)->count();
        $Page = new \Think\Page($count, 10,$_REQUEST);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $list=M('activity')->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->where($map)->select();
        foreach($list as $k=>$v){
            $list[$k]['start_time']=date('Y-m-d',$v['start_time']);
            $list[$k]['end_time']=date('Y-m-d',$v['end_time']);
            if($v['img']){
                $list[$k]['img']=M('picture')->where("id=$v[img]")->getfield('path');
            }
            if($v['status']==1){
                $list[$k]['status_name']='进行中';
            }else{
                $list[$k]['status_name']='已结束';
            }
            $list[$k]['floor_name']=M('floor')->where("id=$v[floor_id]")->getfield('name');
        }
        $show=$Page->show();
        $this->assign('_page',$show);
        $this->assign('cur','活动管理');
        $this->assign('list',$list);
        $this->display();
    }
    //新增活动
    public function activity_add(){
        $me_id=session('me_id');
        // $floor_id=$_SESSION['onethink_home']['me_info']['floor_id'];

        if($_POST && empty($_POST['id'])){
            $data['title']=$_POST['title'];
            $data['start_time']=strtotime($_POST['start_time']);
            $data['end_time']=strtotime($_POST['end_time']);
            $data['bs_time']=strtotime($_POST['bs_time']);
            $data['be_time']=strtotime($_POST['be_time']);
            $data['img']=$_POST['img'];
            $data['content']=$_POST['content'];
            $data['sort']=$_POST['sort'];
            $data['activity_number']=$_POST['activity_number'];
            $data['floor_id']=$_POST['floor_id'];//绑定房源
            $data['me_id']=session('me_id');
            $data['create_time']=time();
            $res=M('activity')->add($data);
            if($res){
                return $this->success('成功',U('Mechanism/activity_index'));
            }else{
                return $this->error('失败',U('Mechanism/activity_index'));
            }
        }elseif($_POST['id']){
            $data['title']=$_POST['title'];
            $data['start_time']=strtotime($_POST['start_time']);
            $data['end_time']=strtotime($_POST['end_time']);
            $data['bs_time']=strtotime($_POST['bs_time']);
            $data['be_time']=strtotime($_POST['be_time']);
            $data['img']=$_POST['img'];
            $data['content']=$_POST['content'];
            $data['sort']=$_POST['sort'];
            $data['activity_number']=$_POST['activity_number'];
            $data['floor_id']=$_POST['floor_id'];//绑定房源
            $data['id']=$_POST['id'];
            $res=M('activity')->save($data);
            if($res){
                return $this->success('成功',U('Mechanism/activity_index'));
            }else{
                return $this->error('失败',U('Mechanism/activity_index'));
            }
        }
        $hid=I('id');
        if($hid){
            //编辑
            $info=M('activity')->where("id=$hid")->find();
            if($info['img']){
                $info['img_src']=M('picture')->where("id=$info[img]")->getfield('path');
            }
            
            $info['start_time']=date('Y-m-d',$info['start_time']);
            $info['end_time']=date('Y-m-d',$info['end_time']);
            $info['bs_time']=date('Y-m-d',$info['bs_time']);
            $info['be_time']=date('Y-m-d',$info['be_time']);
        }
        $floor_list=M('floor')->where("me_id=$me_id")->select();
        $this->assign('floor_list',$floor_list);
        $this->assign('cur','活动管理');
        $this->assign('info',$info);
        $this->display(); 
    }
    //报名管理   活动报名
    public function sign_up(){
        $map['b.me_id']=session('me_id');
        $_REQUEST['title']?$map['b.title']=array('like',"%$_REQUEST[title]%"):'';
        $this->assign('title',$_REQUEST['title']);
        $count = M('sign_up a')->join('left join l_activity b on a.activity_id=b.id')->where($map)->count();
        $Page = new \Think\Page($count, 10,$_REQUEST);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $list=M('sign_up a')->join('left join l_activity b on a.activity_id=b.id')->limit($Page->firstRow . ',' . $Page->listRows)->order('a.id desc')->where($map)->field('a.*,b.title,b.img')->select();
        foreach($list as $k=>$v){
            $list[$k]['create_time']=date('Y-m-d H:i:s',$v['create_time']);
            $list[$k]['img']=M('picture')->where("id=$v[img]")->getfield('path');
        }
        $show=$Page->show();
        $this->assign('_page',$show);
        $this->assign('cur','活动报名');
        $this->assign('list',$list);
        $this->display();
    }
    //删除报名
    public function sign_del(){
        $id=I('id');
        $res=M('sign_up')->where("id=$id")->delete();
        if($res){
            $this->success('成功',U('Mechanism/sign_up'));
        }else{
            $this->error('失败',U('Mechanism/sign_up'));
        }
    }
    //商品管理
    public function good_index(){
        $map['a.is_del']=array('neq',1);
        $map['a.me_id']=session('me_id');//现在是取出这个机构下的商品，可能是只取某楼盘下的商品
        $_REQUEST['name']?$map['a.name']=array('like',"%$_REQUEST[name]%"):'';
        $this->assign('name',$_REQUEST['name']);
        $_REQUEST['status']?$map['a.status']=$_REQUEST['status']:'';
        $this->assign('status',$_REQUEST['status']);        
        $count = M('good a')->join('left join l_good_type b on a.type_id=b.id')->where($map)->count();
        $Page = new \Think\Page($count, 10,$_REQUEST);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $list=M('good a')->join('left join l_good_type b on a.type_id=b.id')->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->where($map)->field('a.*,b.name type_name')->select();
        foreach($list as $k=>$v){
            $list[$k]['create_time']=date('Y-m-d H:i:s',$v['create_time']);
            if($v['status']==1){
                $list[$k]['status_name']='上架中';
            }else{
                $list[$k]['status_name']='已下架';
            }
            if($v['img']){
                $list[$k]['img']=M('picture')->where("id=$v[img]")->getfield('path');
            }
        }
        $show=$Page->show();
        $this->assign('_page',$show);
        $this->assign('cur','商品管理');
        $this->assign('list',$list);
        $this->display();
    }
    public function sj(){
        $data['id']=I('id');
        $data['status']=I('status');
        $res=M('good')->save($data);
        if($res){
            return $this->success('成功',U('Mechanism/good_index'));
        }else{
            return $this->error('失败',U('Mechanism/good_index'));
        }
    }
    //新增 编辑商品
    public function good_add(){
        $floor_id=session('floor_id');//在登录那里，取第一个楼盘进缓存
        $me_id=session('me_id');
        if($_POST && empty($_POST['id'])){
            $data['name']=$_POST['name'];
            $data['type_id']=$_POST['type_id'];
            $data['point']=$_POST['point'];
            $data['img']=$_POST['img'];//这里应该是多图 需要处理一下
            $data['content']=$_POST['content'];
            $data['CDkey']=$_POST['CDkey'];
            $data['stock']=$_POST['stock'];
            // $data['floor_id']=$floor_id;
            $data['floor_id']=$_POST['floor_id'];
            $data['me_id']=session('me_id');
            $data['create_time']=time();
            $data['state']=$_POST['state'];
            $imgs1=$_POST['imgs1'];
            $imgs2=$_POST['imgs2'];
            $imgs3=$_POST['imgs3'];

            $data['imgs']=$imgs1.','.$imgs2.','.$imgs3;

            $res=M('good')->add($data);
            if($res){
                return $this->success('成功',U('Mechanism/good_index'));
            }else{
                return $this->error('失败',U('Mechanism/good_index'));
            }
        }elseif($_POST['id']){
            $data['name']=$_POST['name'];
            $data['type_id']=$_POST['type_id'];
            $data['point']=$_POST['point'];
            $data['img']=$_POST['img'];//这里应该是多图 需要处理一下
            $data['content']=$_POST['content'];
            $data['CDkey']=$_POST['CDkey'];
            $data['stock']=$_POST['stock'];
            // $data['floor_id']=$floor_id;
            $data['floor_id']=$_POST['floor_id'];
            $data['me_id']=session('me_id');
            $data['id']=$_POST['id'];
            $data['state']=$_POST['state'];
            $imgs1=$_POST['imgs1'];
            $imgs2=$_POST['imgs2'];
            $imgs3=$_POST['imgs3'];

            $data['imgs']=$imgs1.','.$imgs2.','.$imgs3;

            $res=M('good')->save($data);
            if($res){
                return $this->success('成功',U('Mechanism/good_index'));
            }else{
                return $this->error('失败',U('Mechanism/good_index'));
            }
        }
        $id=$_REQUEST['id'];
        if($id){
            $info=M('good')->where("id=$id")->find();
            // print_r($info);die;
            if($info['img']){
                $info['img_src']=M('picture')->where("id=$info[img]")->getfield('path');
            }
            if($info['imgs']){
                $imgs=explode(',',$info['imgs']);
                foreach($imgs as $k=>$v){
                    $info['imgg_src'][$k]=M('picture')->where("id=$v")->getfield('path');
                }
                $this->assign('imgs',$imgs);
            }
        }
        $type_list=M('good_type')->where("me_id=$me_id")->select();
        $floor_list=M('floor')->where("me_id=$me_id")->select();
        $this->assign('floor_list',$floor_list);
        $this->assign('type_list',$type_list);
        $this->assign('info',$info);
        $this->assign('cur','商品管理');
        $this->display();
    }
    public function good_del(){
        $id=I('id');
        $res=M('good')->where("id=$id")->save(['is_del'=>1]);
        if($res){
            return $this->success('成功',U('Mechanism/good_index'));
        }else{
            return $this->error('失败',U('Mechanism/good_index'));
        }
    }
    //商品分类
    public function good_type(){

        $map['me_id']=session('me_id');
        $_REQUEST['name1']?$map['name']=array('like',"%$_REQUEST[name1]%"):'';
        $this->assign('name1',$_REQUEST['name1']);
        $count = M('good_type')->where($map)->count();
        $Page = new \Think\Page($count, 10,$_REQUEST);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $list=M('good_type')->where($map)->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->select();
        foreach($list as $k=>$v){
            $list[$k]['create_time']=date('Y-m-d H:i:s',$v['create_time']);
        }
        $show=$Page->show();
        $this->assign('_page',$show);
        $this->assign('list',$list);
        $this->assign('cur','分类管理');
        $this->display();
    }

    public function type_name(){
        $id=I('id');
        $name=M('good_type')->where("id=$id")->getfield('name');
        return api_return(1,'成功',$name);
    }

    public function type_aname(){
        $name=I('name');
        $id=I('id');
        if($id){
            $res=M('good_type')->where("id=$id")->save(['name'=>$name]);
        }else{
            $data['create_time']=time();
            $data['name']=I('name');
            $data['me_id']=session('me_id');
            $res=M('good_type')->add($data);
        }
        if($res){
            return api_return(1,'成功');
        }else{
            return api_return(2,'失败');
        }
        
    }

    //添加 编辑 商品分类(弃用  是弹窗)
    public function type_add(){
        if($_POST && empty($_POST['id'])){
            $data['me_id']=session('me_id');
            $data['name']=$_POST['name'];
            $data['floor_id']=session('floor_id');
            $data['create_time']=time();
            $res=M('good_type')->add($data);
            if($res){
                $this->success('成功',U('Mechanism/good_type'));
            }else{
                $this->error('失败',U('Mechanism/good_type'));
            }
        }elseif($_POST['id']){
            $data['me_id']=session('me_id');
            $data['name']=$_POST['name'];
            $data['floor_id']=session('floor_id');
            $data['id']=$_POST['id'];
            $res=M('good_type')->save($data);
            if($res){
                $this->success('成功',U('Mechanism/good_type'));
            }else{
                $this->error('失败',U('Mechanism/good_type'));
            }
        }
        $id=I('id');
        if($id){
            $info=M('good_type')->where("id=$id")->find();
        }
        $this->assign('info',$info);
        $this->assign('cur','分类管理');
        $this->display();
    }
    //兑换管理
    public function exchange_manage(){
        $map['a.me_id']=session('me_id');//现在是取出这个机构下的商品，可能是只取某楼盘下的商品
        $_REQUEST['name']?$map['b.name']=array('like',"%$_REQUEST[name]%"):'';
        $this->assign('name',$_REQUEST['name']);
        $_REQUEST['status']?$map['a.status']=$_REQUEST['status']:'';
        $this->assign('status',$_REQUEST['status']);
        $count = M('order a')->join('left join l_good b on a.good_id=b.id')->where($map)->count();
        $Page = new \Think\Page($count, 10,$_REQUEST);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $list=M('order a')->join('left join l_good b on a.good_id=b.id')->limit($Page->firstRow . ',' . $Page->listRows)->order('a.id desc')->field('a.*,b.name,b.img,b.type_id')->where($map)->select();
        foreach($list as $k=>$v){
            $list[$k]['create_time']=date('Y-m-d',$v['create_time']);
            if($v['img']){
                $list[$k]['img']=M('picture')->where("id=$v[img]")->getfield('path');
            }
            $list[$k]['type_name']=M('good_type')->where("id=$v[type_id]")->getfield('name');
            if($v['status']==1){
                $list[$k]['status_name']='待审核';
            }elseif($v['status']==2){
                $list[$k]['status_name']='已审核';
            }elseif($v['status']==3){
                $list[$k]['status_name']='待收货';
            }elseif($v['status']==4){
                $list[$k]['status_name']='已自提';
            }elseif($v['status']==5){
                $list[$k]['status_name']='已完成';
            }elseif($v['status']==6){
                $list[$k]['status_name']='已核销';
            }

        }
        $show=$Page->show();
        $this->assign('cur','兑换管理');
        $this->assign('_page',$show);
        $this->assign('list',$list);
        $this->display();
    }
    public function fh(){
        if($_POST){
            $data=$_POST;
            $data['status']=3;
            $res=M('order')->save($data);
            if($res){
                return $this->success('成功',U('exchange_manage'));
            }else{
                return $this->error('失败',U('exchange_manage'));
            }
        }
        $id=I('id');
        $info=M('order')->where("id=$id")->find();
        $express_list=M('shipper')->select();
        // $shipper_id=M('order')->where("id=$id")->getField('shipper_id');
        // $shipper_code=M('order')->where("id=$id")->getField('shipper_code');
        $this->assign('cur','兑换管理');
        $this->assign('express_list',$express_list);
        $this->assign('info',$info);
        $this->display();
    }


    public function ckwl(){

        // $shipper_id=I('shipper_id');
        // $shipper_code=I('shipper_code');
        // print_r($shipper_id);die;
        $Trans=new JscxController($shipper_id,$shipper_code);
        $info=json_decode($Trans,true);
        // print_r($shipper_id);die;
        // $res=$Trans->getOrderTracesByJson($shipper_id,$shipper_code);
        // $info=json_decode($res,true);
    }
    public function sh(){
        $id=I('id');
        $res=M('order')->where("id=$id")->save(['status'=>2]);
        if($res){
            return $this->success('成功',U('Mechanism/exchange_manage'));
        }else{
            return $this->error('失败',U('Mechanism/exchange_manage'));
        }
    }

    public function order_del(){
        $id=I('id');
        $res=M('order')->where("id=$id")->delete();
        if($res){
            return $this->success('成功',U('Mechanism/exchange_manage'));
        }else{
            return $this->error('失败',U('Mechanism/exchange_manage'));
        }
    }
    //用户管理
    public function user_list(){
        // $map['a.me_id']=session('me_id');
        //还是跟进楼盘来
        $map['a.floor_id']=session('floor_id');
        $_REQUEST['mobile']?$map['a.mobile']=array('like',"%$_REQUEST[mobile]%"):'';
        $this->assign('mobile',$_REQUEST['mobile']);
        $_REQUEST['status']?$map['a.status']=$_REQUEST['status']:'';
        $this->assign('status',$_REQUEST['status']);
        $count =M('user a')->join('left join l_floor b on a.floor_id=b.id')->where($map)->count();
        $Page = new \Think\Page($count, 10,$_REQUEST);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $list=M('user a')->join('left join l_floor b on a.floor_id=b.id')->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->field('a.*,b.name')->where($map)->select();
        foreach($list as $k=>$v){
            if($v['img']){
                $list[$k]['img']=M('picture')->where("id=$v[img]")->getfield('path');
            }else{
                $list[$k]['img']='';
            }
            if($v['source']==1){
                $list[$k]['source']='推荐';
            }elseif($v['source']==2){
                $list[$k]['source']='自己上门';
            }elseif($v['source']==3){
                $list[$k]['source']='预约';
            }
            if($v['status']==1){
                $list[$k]['status']='信息录入';
            }elseif($v['status']==2){
                $list[$k]['status']='上访';
            }elseif($v['status']==3){
                $list[$k]['status']='认筹';
            }elseif($v['status']==4){
                $list[$k]['status']='认购';
            }elseif($v['status']==5){
                $list[$k]['status']='签约';
            }elseif($v['status']==6){
                $list[$k]['status']='已失效';
            }
        }
        $show=$Page->show();
        $this->assign('_page',$show);
        $this->assign('list',$list);
        $this->assign('cur','用户管理');
        $this->display();
    }
    //新增用户(弃用)
    public function user_add123(){
        $me_id=session('me_id');
        if($_POST && empty($_POST['id'])){
            $info=M('user')->where("mobile=$_POST[mobile]")->find();
            if($info){
                //这个用户已被推荐或已预约
                $this->error('此用户已存在');
            }
            $data['name']=$_POST['name'];
            $data['img']=$_POST['img'];
            $data['mobile']=$_POST['mobile'];
            $data['address']=$_POST['address'];
            $data['sex']=$_POST['sex'];
            $data['source']=$_POST['source'];
            $data['adviser']=$_POST['adviser'];
            $data['adviser_name']=M('department_staff')->where("id=$_POST[adviser]")->getfield('staff_name');
            $data['class']=$_POST['class'];
            $data['house_type']=$_POST['house_type'];
            $data['min_total']=$_POST['min_total'];
            $data['max_total']=$_POST['max_total'];
            $data['min_area']=$_POST['min_area'];
            $data['max_area']=$_POST['max_area'];
            $data['objective']=$_POST['objective'];
            $data['remark']=$_POST['remark'];
            $data['floor_id']=session('floor_id');
            $data['floor_name']=M('floor')->where("id=$data[floor_id]")->getfield('name');
            $data['me_id']=session('me_id');
            $data['create_time']=time();
            $res=M('user')->add();
            if($res){
                //思考之后还是不添加到会员表

                // $add_data['img']=$_POST['img'];
                // $add_data['user_id']=$res;
                // $add_data['username']=$_POST['name'];
                // $add_data['mobile']=$_POST['mobile'];
                // $add_data['sex']=$_POST['sex'];
                // $re=M('ucenter_member')->add($add_data);
                // if($re){
                //     M('user')->where("id=$res")->save(['b_uid'=>$re]);
                //     $a_data['uid']=$re;
                //     $a_data['sex']=$_POST['sex'];
                //     $a_data['nickname']=$_POST['img'];
                //     M('member')->add($a_data);
                // }
            }
        }elseif($_POST['id']){
            $data['name']=$_POST['name'];
            $data['img']=$_POST['img'];

            //我觉得手机号码都不应该能改
            $data['mobile']=$_POST['mobile'];
            $data['address']=$_POST['address'];
            $data['sex']=$_POST['sex'];
            $data['source']=$_POST['source'];
            $data['adviser']=$_POST['adviser'];
            $data['class']=$_POST['class'];
            $data['house_type']=$_POST['house_type'];
            $data['min_total']=$_POST['min_total'];
            $data['max_total']=$_POST['max_total'];
            $data['min_area']=$_POST['min_area'];
            $data['max_area']=$_POST['max_area'];
            $data['objective']=$_POST['objective'];
            $data['remark']=$_POST['remark'];
            // $data['floor_id']=session('floor_id');
            // $data['me_id']=session('me_id');
            // $data['create_time']=time();
            $res=M('user')->where("id=$_POST[id]")->save($data);
        }
        $id=I('id');
        if($id){
            $info=M('user')->where("id=$id")->find();
            if($info['img']){
                $info['img_src']=M('picture')->where("id=$info[img]")->getField('path');
            }
            if($info['city']){
                $city_list=M('area')->where("id=$info[city]")->select();
                $this->assign('city_list',$city_list);
            }
            if($info['area']){
                $area_list=M('area')->where("id=$info[area]")->select();
                $this->assign('area_list',$area_list);
            }

            $aa=strstr($info['intention_hx'],'1');
            $bb=strstr($info['intention_hx'],'2');
            $cc=strstr($info['intention_hx'],'3');
            $dd=strstr($info['intention_hx'],'4');
            $ee=strstr($info['intention_hx'],'5');
            $this->assign('aa',$aa);
            $this->assign('bb',$bb);
            $this->assign('cc',$cc);
            $this->assign('dd',$dd);
            $this->assign('ee',$ee);
        }
        $floor_list=M('floor')->where("me_id=$me_id")->select();
        $this->assign('floor_list',$floor_list);
        $province_list=M('area')->where('pid=100000')->select();
        $this->assign('province_list',$province_list);
        $this->assign('cur','用户管理');
        $this->assign('info',$info);
        $this->display();
    }


    public function user_add(){
        $me_id=session('me_id');
        if($_POST && empty($_POST['id'])){
            $info=M('user')->where("mobile=$_POST[mobile]")->find();
            if($info){
                //这个用户已被推荐或已预约
                $this->error('此用户已存在');
            }
            // $data['name']=$_POST['name'];
            // $data['img']=$_POST['img'];
            // $data['mobile']=$_POST['mobile'];
            // $data['address']=$_POST['address'];
            // $data['sex']=$_POST['sex'];
            // $data['source']=$_POST['source'];
            // $data['adviser']=$_POST['adviser'];
            // $data['adviser_name']=M('department_staff')->where("id=$_POST[adviser]")->getfield('staff_name');
            // $data['class']=$_POST['class'];
            // $data['house_type']=$_POST['house_type'];
            // $data['min_total']=$_POST['min_total'];
            // $data['max_total']=$_POST['max_total'];
            // $data['min_area']=$_POST['min_area'];
            // $data['max_area']=$_POST['max_area'];
            // $data['objective']=$_POST['objective'];
            // $data['remark']=$_POST['remark'];
            // $data['floor_id']=session('floor_id');

            $data=$_POST;
            $intention_hx=$_POST['intention_hx'];
            unset($data['intention_hx']);
            foreach($intention_hx as $k=>$v){
                if($k==0){
                    $data['intention_hx']=$v;
                }else{
                    $data['intention_hx'].=','.$v;
                }
                
            }
            $data['floor_name']=M('floor')->where("id=$data[floor_id]")->getfield('name');
            $data['adviser_name']=M('department_staff')->where("id=$_POST[adviser]")->getfield('staff_name');
            $data['me_id']=session('me_id');
            $data['create_time']=time();
            $res=M('user')->add($data);
            if($res){
                return $this->success('成功',U('Mechanism/user_list'));
            }else{
                return $this->error('失败',U('Mechanism/user_list'));
            }
        }elseif($_POST['id']){
            // $data['name']=$_POST['name'];
            // $data['img']=$_POST['img'];

            //我觉得手机号码都不应该能改
            // $data['mobile']=$_POST['mobile'];
            // $data['address']=$_POST['address'];
            // $data['sex']=$_POST['sex'];
            // $data['source']=$_POST['source'];
            // $data['adviser']=$_POST['adviser'];
            // $data['class']=$_POST['class'];
            // $data['house_type']=$_POST['house_type'];
            // $data['min_total']=$_POST['min_total'];
            // $data['max_total']=$_POST['max_total'];
            // $data['min_area']=$_POST['min_area'];
            // $data['max_area']=$_POST['max_area'];
            // $data['objective']=$_POST['objective'];
            // $data['remark']=$_POST['remark'];
            // $data['floor_id']=session('floor_id');
            // $data['me_id']=session('me_id');
            // $data['create_time']=time();
            // print_r($_POST);die;
            $data=$_POST;
            $intention_hx=$_POST['intention_hx'];
            unset($data['intention_hx']);
            foreach($intention_hx as $k=>$v){
                if($k==0){
                    $data['intention_hx']=$v;
                }else{
                    $data['intention_hx'].=','.$v;
                }
                
            }
            $data['floor_name']=M('floor')->where("id=$data[floor_id]")->getfield('name');
            $data['adviser_name']=M('department_staff')->where("id=$_POST[adviser]")->getfield('staff_name');

            $res=M('user')->where("id=$_POST[id]")->save($data);
            if($res){
                return $this->success('成功',U('Mechanism/user_list'));
            }else{
                return $this->error('失败',U('Mechanism/user_list'));
            }
        }
        $id=I('id');
        if($id){
            $info=M('user')->where("id=$id")->find();
            if($info['img']){
                $info['img_src']=M('picture')->where("id=$info[img]")->getField('path');
            }
            if($info['city']){
                $city_list=M('area')->where("id=$info[city]")->select();
                $this->assign('city_list',$city_list);
            }
            if($info['area']){
                $area_list=M('area')->where("id=$info[area]")->select();
                $this->assign('area_list',$area_list);
            }
            //置业顾问列表
            $adviser_list=M('department_staff')->where("floor_id=$info[floor_id] && role=1")->select();
            $this->assign('adviser_list',$adviser_list);
            // $aa=strstr($info['intention_hx'],'1');
            // $bb=strstr($info['intention_hx'],'2');
            // $cc=strstr($info['intention_hx'],'3');
            // $dd=strstr($info['intention_hx'],'4');
            // $ee=strstr($info['intention_hx'],'5');
            
            $intention_hx=explode(',',$info['intention_hx']);
            foreach($intention_hx as $k=>$v){
                if ($v == 1) {
                    $aa = 1;
                } else if ($v == 2) {
                    $bb = 1;
                } else if ($v == 3) {
                    $cc = 1;
                } else if ($v == 4) {
                    $dd = 1;
                } 
            }
            $this->assign('aa',$aa);
            $this->assign('bb',$bb);
            $this->assign('cc',$cc);
            $this->assign('dd',$dd);
            $this->assign('ee',$ee);
        }
        $purpose_purchase=M('information_configuration')->where("me_id=$me_id")->getField('purpose_purchase');
        $purpose=explode(',', $purpose_purchase);
        $this->assign('purpose',$purpose);
        $floor_list=M('floor')->where("me_id=$me_id")->select();
        $this->assign('floor_list',$floor_list);
        $province_list=M('area')->where('pid=100000')->select();
        $this->assign('province_list',$province_list);
        $this->assign('cur','用户管理');
        $this->assign('info',$info);
        $this->display();
    }

    //市
    public function city(){
        $id=I('id');
        $list=M('area')->where("pid=$id")->select();
        return api_return(1,'成功',$list);
    }
        //区
    public function area(){
        $id=I('id');
        $list=M('area')->where("pid=$id")->select();
        return api_return(1,'成功',$list);
    }

    //选择置业顾问
    public function adviser_list(){
        $id=I('floor_id');
        $list=M('department_staff')->where("floor_id=$id && role=1")->select();
        return api_return(1,'成功',$list);
    }

    //跟进(弃用)
    public function follow_up(){
        if($_POST){
            $data['user_id']=$_POST['id'];
            $data['content']=$_POST['content'];
            $data['create_time']=time();
            $res=M('follow_up')->add($data);
            if($res){
                $this->success('成功',U('Mechanism/follow_up',array('id'=>$_POST['id'])));
            }else{
                $this->error('失败',U('Mechanism/follow_up',array('id'=>$_POST['id'])));
            }
        }
        $id=I('id');
        $list=M('follow_up')->where("user_id=$id")->select();
        foreach($list as $k=>$v){
            $list[$k]['create_time']=date('Y-m-d H:i:s',$v['create_time']);
        }
        $this->assign('list',$list);
        $this->assign('cur','跟进记录');
        $this->assign('id',$id);
        $this->display();
    }

    //新增跟进
    public function follow_add(){
        if($_POST){
            $data=$_POST;
            $data['create_time']=time();
            $data['follow_time']=strtotime($_POST['follow_time']);
            $res=M('follow_up')->add($data);
            if($res){
                return $this->success('成功',U('user_list'));
            }else{
                return $this->error('失败',U('user_list'));
            }
        }

        //user表的ID
        $userid=I('id');
        $list=M('follow_up')->where("user_id=$userid")->order('id desc')->select();
        foreach($list as $k=>$v){
            $list[$k]['create_time']=date('Y-m-d',$v['create_time']);
            $list[$k]['follow_time']=date('Y-m-d',$v['follow_time']);
            if($v['type']==1){
                $list[$k]['type']='来电';
            }elseif($v['type']==2){
                $list[$k]['type']='去电';
            }elseif($v['type']==3){
                $list[$k]['type']='主动拜访';
            }elseif($v['type']==4){
                $list[$k]['type']='微信';
            }elseif($v['type']==5){
                $list[$k]['type']='其他';
            }
            if($v['status']==1){
                $list[$k]['status']='来访接待';
            }elseif($v['status']==2){
                $list[$k]['status']='认筹';
            }elseif($v['status']==3){
                $list[$k]['status']='认购';
            }elseif($v['status']==4){
                $list[$k]['status']='签约';
            }
        }
        $this->assign('user_id',$userid);
        $this->assign('list',$list);
        $this->assign('cur','用户管理');
        $this->display();
    }
    //认购审核
    public function subscription_review(){
        $this->assign('cur','用户管理');
        $this->display();
    }
    //经纪人管理
    public function agent(){
        $map['me_id']=session('me_id');
        $_REQUEST['mobile']?$map['mobile']=array('like',"%$_REQUEST[mobile]%"):'';
        $this->assign('mobile',$mobile);
        $_REQUEST['status']?$map['status']=$_REQUEST['status']:'';
        if($_REQUEST['status']==3){
            $map['status']=0;
        }
        $this->assign('status',$_REQUEST['status']);
        $this->assign('mobile',$mobile);
        $count = M('agent')->where($map)->count();
        $Page = new \Think\Page($count, 10,$_REQUEST);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $list=M('agent')->limit($Page->firstRow.','.$Page->listRows)->where($map)->order('id desc')->select();
        foreach($list as $k=>$v){
            $list[$k]['create_time']=date('Y-m-d',$v['create_time']);
            if($v['img']){
                //小程序端申请经纪人，我是把微信头像弄过来
                if(is_numeric($v['img'])){
                    $list[$k]['img']=M('picture')->where("id=$v[img]")->getfield('path');
                }else{
                    $list[$k]['img']=$v['img'];                    
                }
                
            }else{
                $list[$k]['img']='';
            }
            if($v['status']==0){
                $list[$k]['status_name']='未审核';
            }elseif($v['status']==1){
                $list[$k]['status_name']='审核通过';
            }elseif($v['status']==2){
                $list[$k]['status_name']='审核失败';
            }
        }
        $show=$Page->show();
        $this->assign('cur','经纪人管理');
        $this->assign('list',$list);
        $this->assign('_page',$show);
        $this->display();
    }
    //新增 编辑经纪人
    public function agent_add(){
        if($_POST && empty($_POST['id'])){
            // $data['img']=$_POST['img'];
            // $data['name']=$_POST['name'];
            // $data['mobile']=$_POST['mobile'];
            // $data['address']=$_POST['address'];
            // $data['sex']=$_POST['sex'];
            // $data['email']=$_POST['email'];
            // $data['occupation']=$_POST['occupation'];
            // $data['id_number']=$_POST['id_number'];
            // $data['status']=$_POST['status'];
            // $data['create_time']=time();

            $data=$_POST;
            $data['create_time']=time();
            $data['me_id']=session('me_id');
            $res=M('agent')->add($data);
            if($res){
                return $this->success('成功',U('Mechanism/agent'));
            }else{
                return $this->error('失败',U('Mechanism/agent'));
            }
        }elseif($_POST['id']){
            // $data['img']=$_POST['img'];
            // $data['name']=$_POST['name'];
            // $data['mobile']=$_POST['mobile'];
            // $data['address']=$_POST['address'];
            // $data['sex']=$_POST['sex'];
            // $data['email']=$_POST['email'];
            // $data['occupation']=$_POST['occupation'];
            // $data['id_number']=$_POST['id_number'];
            // $data['status']=$_POST['status'];
            // $data['id']=$_POST['id'];

            $data=$_POST;
            $res=M('agent')->save($data);
            if($res){
                return $this->success('成功',U('Mechanism/agent'));
            }else{
                return $this->error('失败',U('Mechanism/agent'));
            }
        }
        $id=I('id');
        if($id){
            $info=M('agent')->where("id=$id")->find();
            // if($info['img']){
            //     $info['img_src']=M('picture')->where("id=$info[img]")->getField('path');
            // }

            if($info['img']){
                //小程序端申请经纪人，我是把微信头像弄过来
                if(is_numeric($info['img'])){
                    $info['img_src']=M('picture')->where("id=$info[img]")->getfield('path');
                }else{
                    $info['img_src']=$info['img'];                    
                }
                
            }else{
                $list[$k]['img']='';
            }


            if($info['id_card_z']){
                $info['id_card_z_src']=M('picture')->where("id=$info[id_card_z]")->getField('path');
            }
            if($info['id_card_f']){
                $info['id_card_f_src']=M('picture')->where("id=$info[id_card_f]")->getField('path');
            }
            if($info['city']){
                $city_list=M('area')->where("id=$info[city]")->select();
                $this->assign('city_list',$city_list);
            }
            if($info['area']){
                $area_list=M('area')->where("id=$info[area]")->select();
                $this->assign('area_list',$area_list);
            }
        }
        $province_list=M('area')->where('pid=100000')->select();
        $this->assign('province_list',$province_list);
        $this->assign('info',$info);
        $this->assign('cur','经纪人管理');
        $this->display();
    }
    //认购审核

    //数据管理
    public function data_management(){
        //不确定按机构来统计，还是楼盘
        $floor_id=session('floor_id');
        //客户统计
        //今天录入信息量
        $bbtime=date('Y-m-d',time());//今天
        $ebtime=date('Y-m-d',strtotime("+1 day"));//明天
        $btime=strtotime($bbtime);
        $etime=strtotime($ebtime);
        $condition['create_time']=array('between',"$btime,$etime");
        $condition['floor_id']=$floor_id;
        $condtion['status']=1;
        $enter_today=M('user')->where($condition)->count();
        //昨日录入信息量
        unset($condition);
        $bbtime=date('Y-m-d',strtotime("-1 day"));
        $ebtime=date('Y-m-d',time());//今天
        $btime=strtotime($bbtime);
        $etime=strtotime($ebtime);
        $condition['create_time']=array('between',"$btime,$etime");
        $condition['floor_id']=$floor_id;
        $condtion['status']=1;
        $entered_yesterday=M('user')->where($condition)->count();
        //日环比
        if($entered_yesterday){
            $input_link_comparison=($enter_today-$entered_yesterday)/$entered_yesterday*100;
        }else{
            $input_link_comparison=($enter_today-$entered_yesterday)/1*100;
        }
        
        $this->assign('enter_today',$enter_today);
        $this->assign('entered_yesterday',$entered_yesterday);
        $this->assign('input_link_comparison',$input_link_comparison);

        //今日上访量
        unset($condition);
        $bbtime=date('Y-m-d',time());//今天
        $ebtime=date('Y-m-d',strtotime("+1 day"));//明天
        $btime=strtotime($bbtime);
        $etime=strtotime($ebtime);
        $condition['petition_time']=array('between',"$btime,$etime");
        $condition['floor_id']=$floor_id;
        $condtion['status']=2;
        $petition_today=M('user')->where($condition)->count();
        //昨日上访
        unset($condition);
        $bbtime=date('Y-m-d',strtotime("-1 day"));
        $ebtime=date('Y-m-d',time());//今天
        $btime=strtotime($bbtime);
        $etime=strtotime($ebtime);
        $condition['petition_time']=array('between',"$btime,$etime");
        $condition['floor_id']=$floor_id;
        $condtion['status']=2;
        $petition_yesterday=M('user')->where($condition)->count();
        //日环比
        if($petition_yesterday){
            $input_link_comparison1=($petition_today-$petition_yesterday)/$petition_yesterday*100;
        }else{
            $input_link_comparison1=($petition_today-$petition_yesterday)/1*100;
        }
        
        $this->assign('petition_today',$petition_today);
        $this->assign('petition_yesterday',$petition_yesterday);
        $this->assign('input_link_comparison1',$input_link_comparison1);

        //今日认筹量
        unset($condition);
        $bbtime=date('Y-m-d',time());//今天
        $ebtime=date('Y-m-d',strtotime("+1 day"));//明天
        $btime=strtotime($bbtime);
        $etime=strtotime($ebtime);
        $condition['funding_time']=array('between',"$btime,$etime");
        $condition['floor_id']=$floor_id;
        $condtion['status']=3;
        $raise_money_today=M('user')->where($condition)->count();
        //昨日认筹量
        unset($condition);
        $bbtime=date('Y-m-d',strtotime("-1 day"));
        $ebtime=date('Y-m-d',time());//今天
        $btime=strtotime($bbtime);
        $etime=strtotime($ebtime);
        $condition['funding_time']=array('between',"$btime,$etime");
        $condition['floor_id']=$floor_id;
        $condtion['status']=3;
        $raise_money_yesterday=M('user')->where($condition)->count();
        //日环比
        if($input_link_comparison2){
            $input_link_comparison2=($raise_money_today-$raise_money_yesterday)/$raise_money_yesterday*100;
        }else{
            $input_link_comparison2=($raise_money_today-$raise_money_yesterday)/1*100;
        }
        
        $this->assign('raise_money_today',$raise_money_today);
        $this->assign('raise_money_yesterday',$raise_money_yesterday);
        $this->assign('input_link_comparison2',$input_link_comparison2);


        //今日认购量
        unset($condition);
        $bbtime=date('Y-m-d',time());//今天
        $ebtime=date('Y-m-d',strtotime("+1 day"));//明天
        $btime=strtotime($bbtime);
        $etime=strtotime($ebtime);
        $condition['subscription_time']=array('between',"$btime,$etime");
        $condition['floor_id']=$floor_id;
        $condtion['status']=4;
        $subscribe_today=M('user')->where($condition)->count();
        //昨日认购量
        unset($condition);
        $bbtime=date('Y-m-d',strtotime("-1 day"));
        $ebtime=date('Y-m-d',time());//今天
        $btime=strtotime($bbtime);
        $etime=strtotime($ebtime);
        $condition['subscription_time']=array('between',"$btime,$etime");
        $condition['floor_id']=$floor_id;
        $condtion['status']=4;
        $subscribe_yesterday=M('user')->where($condition)->count();
        //日环比
        if($input_link_comparison3){
            $input_link_comparison3=($subscribe_today-$subscribe_yesterday)/$subscribe_yesterday*100;
        }else{
            $input_link_comparison3=($subscribe_today-$subscribe_yesterday)/1*100;
        }
        
        $this->assign('subscribe_today',$subscribe_today);
        $this->assign('subscribe_yesterday',$subscribe_yesterday);
        $this->assign('input_link_comparison3',$input_link_comparison3);

        //今日签约量量
        unset($condition);
        $bbtime=date('Y-m-d',time());//今天
        $ebtime=date('Y-m-d',strtotime("+1 day"));//明天
        $btime=strtotime($bbtime);
        $etime=strtotime($ebtime);
        $condition['signing_time']=array('between',"$btime,$etime");
        $condition['floor_id']=$floor_id;
        $condtion['status']=5;
        $signing_today=M('user')->where($condition)->count();
        //昨日签约量
        unset($condition);
        $bbtime=date('Y-m-d',strtotime("-1 day"));
        $ebtime=date('Y-m-d',time());//今天
        $btime=strtotime($bbtime);
        $etime=strtotime($ebtime);
        $condition['signing_time']=array('between',"$btime,$etime");
        $condition['floor_id']=$floor_id;
        $condtion['status']=5;
        $signing_yesterday=M('user')->where($condition)->count();
        //日环比
        if($input_link_comparison4){
            $input_link_comparison4=($signing_today-$signing_yesterday)/$signing_yesterday*100;
        }else{
            $input_link_comparison4=($signing_today-$signing_yesterday)/1*100;
        }
        
        $this->assign('signing_today',$signing_today);
        $this->assign('signing_yesterday',$signing_yesterday);
        $this->assign('input_link_comparison4',$input_link_comparison4);



        $map['floor_id']=$floor_id;
        // $_REQUEST['name']?$map['adviser_name']=array('like',"%$_REQUEST[name]%"):'';
        // $_REQUEST['source']?$map['source']=$_REQUEST['source']:'';
        $_REQUEST['name']?$map['name']=array('like',"%$_REQUEST[name]%"):'';
        $this->assign('name',$_REQUEST['name']);
        $_REQUEST['status']?$map['status']=$_REQUEST['status']:'';
        $this->assign('status',$_REQUEST['status']);
        $count = M('user')->where($map)->count();
        $Page = new \Think\Page($count, 10,$_REQUEST);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $list=M('user')->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->where($map)->select();
        foreach($list as $k=>$v){
            if($v['status']==2){
                $list[$k]['status_name']='上访';
            }elseif($v['status']==3){
                $list[$k]['status_name']='认筹';
            }elseif($v['status']==4){
                $list[$k]['status_name']='认购';
            }elseif($v['status']==5){
                $list[$k]['status_name']='签约';
            }elseif($v['status']==1){
                $list[$k]['status_name']='信息录入';
            }
            if($v['img']){
                $list[$k]['img']=M('picture')->where("id=$v[img]")->getField('path');
            }
            //预约 到时页面出来再看
        }
        $show=$Page->show();
        $this->assign('_page',$show);
        $this->assign('cur','客户统计');
        $this->assign('list',$list);
        $this->display();
    }

    //统计数据
    public function statistical_data(){
        $me_id=session('me_id');
        // print_r(session('floor_id'));die;
        $map['floor_id']=session('floor_id');//后面要根据选择的楼盘，现在是取缓存
        // print_r(session('floor_id'));die;
        $_REQUEST['floor_id']?$map['floor_id']=$_REQUEST['floor_id']:'';
        $this->assign('fl_id',$map['floor_id']);
        $_REQUEST['type']?$bb=$_REQUEST['type']:$bb=1;
        $this->assign('bb',$bb);
        //默认是日   $bb=2的时候是月
        if($bb==1){
            //日统计
            $start_time = mktime(0,0,0,date('m'),1,date('Y'));
            $end_time = time();

            $start_date=date('Y-m-d',$start_time);
            $end_date=date('Y-m-d',$end_time);
            if(!empty($start_date))
                $start_date=date('Y-m-d',strtotime($start_date));
            else
                $start_date=date('Y-m-d',time());

            if(!empty($end_date))
                $end_date=date('Y-m-d',strtotime($end_date));
            else
                $end_date=date('Y-m-d',strtotime('+1 day'));


            //between 查询 要求必须是从低到高
            if($start_date>$end_date)
            {
                $ttt=$start_date;
                $start_date=$end_date;
                $end_date=$ttt;
            }elseif($start_date==$end_date){
                echo '时间输入错误';die;
            }
            $time_s=strtotime($start_date);
            $time_e=strtotime($end_date);

            $days_inline_array=array();
            $times_inline_array=array();
            $days_list=array();
            $days_inline_array[0]=$start_date;
            $times_inline_array[0]=$time_s;
            if(
                date('Y-m-d',$time_s)
                ==date('Y-m-d',$time_e)
            ){
                $days_inline_array[1]=$end_date;
                $times_inline_array[1]=$time_e;
                $days_list[]=date('Y-m-d',$time_s);
            }
            else
            {
                $A_temp=date('Y-m-d',$time_s+86400);
                $A=strtotime($A_temp);
                $B=$time_e-$A;
                $C_quotient=floor($B/86400);    //商舍去法取整
                $C_remainder=fmod($B,86400);               //余数

                $days_inline_array[1]=$A_temp;
                $times_inline_array[1]=$A;
                $days_list[]=date('Y-m-d',$A);
                for($increase_time=$A,$c_count_t=1;$c_count_t<=$C_quotient;$c_count_t++)
                {
                    $increase_time+=86400;
                    $days_inline_array[]=date('Y-m-d',$increase_time);
                    $times_inline_array[]=$increase_time;
                    $days_list[]=date('Y-m-d',$increase_time);
                }
                if($C_remainder)
                {
                    $days_inline_array[]=$end_date;
                    $times_inline_array[]=$time_e;
                }
            }
            // print_r($days_inline_array);die;
            foreach($days_inline_array as $k=>$v){
                if($k==0){
                    $line_time=$v;
                }else{
                    $line_time.=','.$v;
                }
                $btime=strtotime($v);
                $etime=$btime+86400;

                //上访时间
                $map['petition_time']=array('between',"$btime,$etime");

                
                // print_r($map);die;
                $ccc[$k]=M('user')->where($map)->count();

            }
            $ccc[$k]=M('user')->where($map)->count();



        }elseif($bb==2){
            //月统计
            $currentTime = time();
            $cyear = floor(date("Y",$currentTime));
            $cMonth = floor(date("m",$currentTime));
              
            for($i=0;$i<12;$i++){
                $nMonth = $cMonth-$i;
                $cyear = $nMonth == 0 ? ($cyear-1) : $cyear;
                $nMonth = $nMonth <= 0 ? 12+$nMonth : $nMonth;
                $date = $cyear."-".$nMonth."-1";

                $firstday1 = date('Y-m', strtotime($date));
                $lastday1 = date('Y-m-t', strtotime($date));
                $date1=strtotime($lastday1." 23:59:59");

                if($i==0){
                    $line_time=$firstday1;
                }else{
                    $line_time.=','.$firstday1;
                }

                $firstday = strtotime($date);
 
                //上访时间
                $map['petition_time']=array('between',"$firstday,$date1");

                // print_r($map);die;
                $ccc[$i]=M('user')->where($map)->count();

            }
            
        }


        unset($map['petition_time']);
        foreach($ccc as $k=>$v){
            if($k==0){
                $bbb=$v;
            }else{
                $bbb.=','.$v;
            }
        }
        $this->assign('bbb',$bbb);
        $this->assign('line_time',$line_time);






        $this->assign('floor_id',$map['floor_id']);
        //认筹
        $map['status']=3;
        $raise_count=M('user')->where($map)->count();
        $this->assign('raise_count',$raise_count);
        $raise_money=M('user')->where($map)->limit(0,3)->order('id desc')->select();
        //认购
        $map['status']=4;
        $subscription_count=M('user')->where($map)->count();
        $this->assign('subscription_count',$subscription_count);
        $subscription=M('user')->where($map)->limit(0,3)->order('id desc')->select();
        //签约
        $map['status']=5;
        $sign_count=M('user')->where($map)->count();
        $this->assign('sign_count',$sign_count);
        $sign_contract=M('user')->where($map)->limit(0,3)->order('id desc')->select();

        //今日跟进
        $time=time();
        $tt=date('Y-m-d',time());
        $t=strtotime($tt);
        $condition['create_time']=array('between',"$t,$time");
        $condition['floor_id']=$map['floor_id'];
        $follow_count=M('follow_up')->where($condition)->count();
        $this->assign('follow_count',$follow_count);
        //今日忽略
        $time=time();
        $tt=date('Y-m-d',time());
        $t=strtotime($tt);
        $where['invalid_time']=array('between',"$t,$time");
        $where['floor_id']=$map['floor_id'];
        $invalid_count=M('user')->where($where)->count();
        $this->assign('invalid_count',$invalid_count);

        $floor_list=M('floor')->where("me_id=$me_id")->select();
        $this->assign('floor_list',$floor_list);
        $this->assign('raise_money',$raise_money);
        $this->assign('subscription',$subscription);
        $this->assign('sign_contract',$sign_contract);
        $this->assign('cur','统计数据');
        $this->display();
    }
    //认筹 更多
    public function more_raise(){
        $map['floor_id']=session('floor_id');//后面要根据选择的楼盘，现在是取缓存
        $_REQUEST['mobile']?$map['mobile']=array('like',"%$_REQUEST[mobile]%"):'';
        $this->assign('mobile',$_REQUEST['mobile']);
        //认筹
        $map['status']=3;

        $count = M('user')->where($map)->count();
        $Page = new \Think\Page($count, 10,$_REQUEST);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $list=M('user')->where($map)->limit(0,3)->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->select();
        $show=$Page->show();
        $this->assign('_page',$show);
        $this->assign('list',$list);
        $this->assign('cur','统计数据');
        $this->display();
    }
    //认购 更多
    public function more_subscription(){
        $map['floor_id']=session('floor_id');//后面要根据选择的楼盘，现在是取缓存
        $_REQUEST['mobile']?$map['mobile']=array('like',"%$_REQUEST[mobile]%"):'';
        $this->assign('mobile',$_REQUEST['mobile']);
        //认筹
        $map['status']=4;

        $count = M('user')->where($map)->count();
        $Page = new \Think\Page($count, 10,$_REQUEST);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $list=M('user')->where($map)->limit(0,3)->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->select();
        $show=$Page->show();
        $this->assign('_page',$show);
        $this->assign('list',$list);
        $this->assign('cur','统计数据');
        $this->display();
    }
    //签约 更多
    public function more_sign(){
        $map['floor_id']=session('floor_id');//后面要根据选择的楼盘，现在是取缓存
        $_REQUEST['mobile']?$map['mobile']=array('like',"%$_REQUEST[mobile]%"):'';
        $this->assign('mobile',$_REQUEST['mobile']);
        //来访客户
        //M('user')->where()


        //认筹
        $map['status']=5;
        $count = M('user')->where($map)->count();
        $Page = new \Think\Page($count, 10,$_REQUEST);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $list=M('user')->where($map)->limit(0,3)->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->select();
        $show=$Page->show();
        $this->assign('_page',$show);
        $this->assign('list',$list);
        $this->assign('cur','统计数据');
        $this->display();
    }
    //业绩排名
    public function achievement(){
        $me_id=session('me_id');
        $map['floor_id']=session('floor_id');//后面要根据选择的楼盘，现在是取缓存
        $_REQUEST['floor_id']?$map['floor_id']=$_REQUEST['floor_id']:'';
        $this->assign('floor_id',$map['floor_id']);

        // $_REQUEST['status']?$map['status']=$_REQUEST['status']:$map['status']=1;
        $_REQUEST['status']?$aa=$_REQUEST['status']:$aa=1;
        $this->assign('aa',$aa);

        // unset($map['status']);
        //日
        $_REQUEST['type']?$bb=$_REQUEST['type']:$bb=1;
        $this->assign('bb',$bb);
        //默认是日   $bb=2的时候是月
        if($bb==1){
            //日统计
            $start_time = mktime(0,0,0,date('m'),1,date('Y'));
            $end_time = time();

            $start_date=date('Y-m-d',$start_time);
            $end_date=date('Y-m-d',$end_time);
            if(!empty($start_date))
                $start_date=date('Y-m-d',strtotime($start_date));
            else
                $start_date=date('Y-m-d',time());

            if(!empty($end_date))
                $end_date=date('Y-m-d',strtotime($end_date));
            else
                $end_date=date('Y-m-d',strtotime('+1 day'));


            //between 查询 要求必须是从低到高
            if($start_date>$end_date)
            {
                $ttt=$start_date;
                $start_date=$end_date;
                $end_date=$ttt;
            }elseif($start_date==$end_date){
                echo '时间输入错误';die;
            }
            $time_s=strtotime($start_date);
            $time_e=strtotime($end_date);

            $days_inline_array=array();
            $times_inline_array=array();
            $days_list=array();
            $days_inline_array[0]=$start_date;
            $times_inline_array[0]=$time_s;
            if(
                date('Y-m-d',$time_s)
                ==date('Y-m-d',$time_e)
            ){
                $days_inline_array[1]=$end_date;
                $times_inline_array[1]=$time_e;
                $days_list[]=date('Y-m-d',$time_s);
            }
            else
            {
                $A_temp=date('Y-m-d',$time_s+86400);
                $A=strtotime($A_temp);
                $B=$time_e-$A;
                $C_quotient=floor($B/86400);    //商舍去法取整
                $C_remainder=fmod($B,86400);               //余数

                $days_inline_array[1]=$A_temp;
                $times_inline_array[1]=$A;
                $days_list[]=date('Y-m-d',$A);
                for($increase_time=$A,$c_count_t=1;$c_count_t<=$C_quotient;$c_count_t++)
                {
                    $increase_time+=86400;
                    $days_inline_array[]=date('Y-m-d',$increase_time);
                    $times_inline_array[]=$increase_time;
                    $days_list[]=date('Y-m-d',$increase_time);
                }
                if($C_remainder)
                {
                    $days_inline_array[]=$end_date;
                    $times_inline_array[]=$time_e;
                }
            }
            // print_r($days_inline_array);die;
            foreach($days_inline_array as $k=>$v){
                if($k==0){
                    $line_time=$v;
                }else{
                    $line_time.=','.$v;
                }
                $btime=strtotime($v);
                $etime=$btime+86400;
                if($aa==1){
                    //信息录入时间
                    $map['create_time']=array('between',"$btime,$etime");
                }elseif($aa==2){
                    //上访时间
                    $map['petition_time']=array('between',"$btime,$etime");
                }elseif($aa==3){
                    //认筹时间
                    $map['funding_time']=array('between',"$btime,$etime");
                }elseif($aa==4){
                    //认购时间
                    $map['subscription_time']=array('between',"$btime,$etime");
                }elseif($aa==5){
                    //签约时间
                    $map['signing_time']=array('between',"$btime,$etime");
                }
                
                // print_r($map);die;
                $ccc[$k]=M('user')->where($map)->count();

            }
            $ccc[$k]=M('user')->where($map)->count();



        }elseif($bb==2){
            //月统计
            $currentTime = time();
            $cyear = floor(date("Y",$currentTime));
            $cMonth = floor(date("m",$currentTime));
              
            for($i=0;$i<12;$i++){
                $nMonth = $cMonth-$i;
                $cyear = $nMonth == 0 ? ($cyear-1) : $cyear;
                $nMonth = $nMonth <= 0 ? 12+$nMonth : $nMonth;
                $date = $cyear."-".$nMonth."-1";

                // $firstday1 = date('Y-m-01', strtotime($date));
                $firstday1 = date('Y-m', strtotime($date));
                $lastday1 = date('Y-m-t', strtotime($date));
                $date1=strtotime($lastday1." 23:59:59");

                if($i==0){
                    $line_time=$firstday1;
                }else{
                    $line_time.=','.$firstday1;
                }

                $firstday = strtotime($date);
 
             
                if($aa==1){
                    //信息录入时间
                    $map['create_time']=array('between',"$firstday,$date1");
                }elseif($aa==2){
                    //上访时间
                    $map['petition_time']=array('between',"$firstday,$date1");
                }elseif($aa==3){
                    //认筹时间
                    $map['funding_time']=array('between',"$firstday,$date1");
                }elseif($aa==4){
                    //认购时间
                    $map['subscription_time']=array('between',"$firstday,$date1");
                }elseif($aa==5){
                    //签约时间
                    $map['signing_time']=array('between',"$firstday,$date1");
                }
                
                // print_r($map);die;
                $ccc[$i]=M('user')->where($map)->count();

            }
            
        }

        $list=M('user')->where($map)->group('adviser')->field("*,count(adviser) cc")->select();
        $this->assign('list',$list);

        foreach($ccc as $k=>$v){
            if($k==0){
                $bbb=$v;
            }else{
                $bbb.=','.$v;
            }
        }
        $this->assign('bbb',$bbb);
        $this->assign('line_time',$line_time);
        $floor_list=M('floor')->where("me_id=$me_id")->select();
        $this->assign('floor_list',$floor_list);
        $this->assign('cur','业绩排名');
        $this->display();
    }
    //转化情况
    public function conversion(){
        $me_id=session('me_id');
        $map['floor_id']=session('floor_id');//后面要根据选择的楼盘，现在是取缓存
        $_REQUEST['floor_id']?$map['floor_id']=$_REQUEST['floor_id']:'';
        $this->assign('floor_id',$map['floor_id']);

        // $_REQUEST['status']?$map['status']=$_REQUEST['status']:$map['status']=1;
        $_REQUEST['status']?$aa=$_REQUEST['status']:$aa=1;
        $this->assign('aa',$aa);

        // unset($map['status']);
        //日
        $_REQUEST['type']?$bb=$_REQUEST['type']:$bb=1;
        $this->assign('bb',$bb);
        //默认是日   $bb=2的时候是月
        if($bb==1){
            //日统计
            $start_time = mktime(0,0,0,date('m'),1,date('Y'));
            $end_time = time();

            $start_date=date('Y-m-d',$start_time);
            $end_date=date('Y-m-d',$end_time);
            if(!empty($start_date))
                $start_date=date('Y-m-d',strtotime($start_date));
            else
                $start_date=date('Y-m-d',time());

            if(!empty($end_date))
                $end_date=date('Y-m-d',strtotime($end_date));
            else
                $end_date=date('Y-m-d',strtotime('+1 day'));


            //between 查询 要求必须是从低到高
            if($start_date>$end_date)
            {
                $ttt=$start_date;
                $start_date=$end_date;
                $end_date=$ttt;
            }elseif($start_date==$end_date){
                echo '时间输入错误';die;
            }
            $time_s=strtotime($start_date);
            $time_e=strtotime($end_date);

            $days_inline_array=array();
            $times_inline_array=array();
            $days_list=array();
            $days_inline_array[0]=$start_date;
            $times_inline_array[0]=$time_s;
            if(
                date('Y-m-d',$time_s)
                ==date('Y-m-d',$time_e)
            ){
                $days_inline_array[1]=$end_date;
                $times_inline_array[1]=$time_e;
                $days_list[]=date('Y-m-d',$time_s);
            }
            else
            {
                $A_temp=date('Y-m-d',$time_s+86400);
                $A=strtotime($A_temp);
                $B=$time_e-$A;
                $C_quotient=floor($B/86400);    //商舍去法取整
                $C_remainder=fmod($B,86400);               //余数

                $days_inline_array[1]=$A_temp;
                $times_inline_array[1]=$A;
                $days_list[]=date('Y-m-d',$A);
                for($increase_time=$A,$c_count_t=1;$c_count_t<=$C_quotient;$c_count_t++)
                {
                    $increase_time+=86400;
                    $days_inline_array[]=date('Y-m-d',$increase_time);
                    $times_inline_array[]=$increase_time;
                    $days_list[]=date('Y-m-d',$increase_time);
                }
                if($C_remainder)
                {
                    $days_inline_array[]=$end_date;
                    $times_inline_array[]=$time_e;
                }
            }
            // print_r($days_inline_array);die;
            foreach($days_inline_array as $k=>$v){
                if($k==0){
                    $line_time=$v;
                }else{
                    $line_time.=','.$v;
                }
                $btime=strtotime($v);
                $etime=$btime+86400;
                if($aa==1){
                    //信息转上访时间
                    $map['petition_time']=array('between',"$btime,$etime");
                }elseif($aa==2){
                    //上访转认筹时间
                    $map['funding_time']=array('between',"$btime,$etime");
                }elseif($aa==3){
                    //上访转认购
                    $map['funding_time']=array('eq','');
                    $map['subscription_time']=array('between',"$btime,$etime");
                }elseif($aa==4){
                    //认筹转认购时间
                    $map['subscription_time']=array('between',"$btime,$etime");
                }elseif($aa==5){
                    //认购转签约时间
                    $map['signing_time']=array('between',"$btime,$etime");
                }
                
                // print_r($map);die;
                $ccc[$k]=M('user')->where($map)->count();

            }
            $ccc[$k]=M('user')->where($map)->count();



        }elseif($bb==2){
            //月统计
            $currentTime = time();
            $cyear = floor(date("Y",$currentTime));
            $cMonth = floor(date("m",$currentTime));
              
            for($i=0;$i<12;$i++){
                $nMonth = $cMonth-$i;
                $cyear = $nMonth == 0 ? ($cyear-1) : $cyear;
                $nMonth = $nMonth <= 0 ? 12+$nMonth : $nMonth;
                $date = $cyear."-".$nMonth."-1";

                // $firstday1 = date('Y-m-01', strtotime($date));
                $firstday1 = date('Y-m', strtotime($date));
                $lastday1 = date('Y-m-t', strtotime($date));
                $date1=strtotime($lastday1." 23:59:59");

                if($i==0){
                    $line_time=$firstday1;
                }else{
                    $line_time.=','.$firstday1;
                }

                $firstday = strtotime($date);
 
             
                if($aa==1){
                    //信息转上访时间
                    $map['petition_time']=array('between',"$firstday,$date1");
                }elseif($aa==2){
                    //上访转认筹
                    $map['funding_time']=array('between',"$firstday,$date1");
                }elseif($aa==3){
                    //上访转认购 (这里就是跟业绩排名的区别)
                    $map['funding_time']=array('eq','');
                    $map['subscription_time']=array('between',"$firstday,$date1");
                }elseif($aa==4){
                    //认筹转认购时间
                    $map['subscription_time']=array('between',"$firstday,$date1");
                }elseif($aa==5){
                    //认购转签约时间
                    $map['signing_time']=array('between',"$firstday,$date1");
                }
                
                // print_r($map);die;
                $ccc[$i]=M('user')->where($map)->count();

            }
            
        }


        $floor_list=M('floor')->where("me_id=$me_id")->select();
        foreach($ccc as $k=>$v){
            if($k==0){
                $bbb=$v;
            }else{
                $bbb.=','.$v;
            }
        }
        $this->assign('bbb',$bbb);
        $this->assign('line_time',$line_time);
        $this->assign('floor_list',$floor_list);
        $this->assign('cur','转化情况');
        $this->display();
    }
    //用户浏览统计
    public function browse(){
        $floor_id=session('floor_id');
        // print_r($floor_id);die;
        $count = M('browse')->where("floor_id=$floor_id")->count();
        $Page = new \Think\Page($count, 10,$_REQUEST);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $list=M('browse a')->join('left join l_ucenter_member b on a.uid=b.id')->limit($Page->firstRow . ',' . $Page->listRows)->order('a.id desc')->where("a.floor_id=$floor_id")->field('a.*,b.*,a.create_time ctime')->select();
        // print_r($list);die;
        foreach($list as $k=>$v){
            $list[$k]['ctime']=date('Y-m-d H:i:s',$v['ctime']);
            if(is_numeric($v['img'])){
                //不是整型的话就是微信头像，是一个地址，不需要处理
                $list[$k]['img']=M('picture')->where("id=$v[img]")->getfield('path');
            } 
            if($v['sex']==1){
                $list[$k]['sex']=='男';
            }else{
                $list[$k]['sex']=='女';
            }
        }


        //今天录入信息量
        $bbtime=date('Y-m-d',time());//今天
        $ebtime=date('Y-m-d',strtotime("+1 day"));//明天
        $btime=strtotime($bbtime);
        $etime=strtotime($ebtime);
        $condition['create_time']=array('between',"$btime,$etime");
        $condition['floor_id']=$floor_id;
        $enter_today=M('browse')->where($condition)->count();
        //昨日录入信息量
        unset($condition);
        $bbtime=date('Y-m-d',strtotime("-1 day"));
        $ebtime=date('Y-m-d',time());//今天
        $btime=strtotime($bbtime);
        $etime=strtotime($ebtime);
        $condition['create_time']=array('between',"$btime,$etime");
        $condition['floor_id']=$floor_id;
        $entered_yesterday=M('browse')->where($condition)->count();
        //增长
        if($entered_yesterday){
            $input_link_comparison=($enter_today-$entered_yesterday)/$entered_yesterday*100;
        }else{
            $input_link_comparison=($enter_today-$entered_yesterday)/1*100;
        }

        $show=$Page->show();
        $this->assign('input_link_comparison',$input_link_comparison);
        $this->assign('enter_today',$enter_today);
        $this->assign('entered_yesterday',$entered_yesterday);
        $this->assign('_page',$show);
        $this->assign('list',$list);
        $this->assign('cur','用户浏览统计');
        $this->display();
    }
    //房源统计
    public function housing_statistics(){
        $map['floor_id']=2;
        // $map['status']=1;
        $list=M('generate_room')->where($map)->group('build_id')->select();
        foreach($list as $k=>$v){
            //待售
            $list[$k]['status1']=M('generate_room')->where("build_id=$v[build_id] && status=1")->count();
            //在售
            $list[$k]['status2']=M('generate_room')->where("build_id=$v[build_id] && status=2")->count();
            //已售
            $list[$k]['status3']=M('generate_room')->where("build_id=$v[build_id] && status=3")->count();
            //预留
            $list[$k]['status4']=M('generate_room')->where("build_id=$v[build_id] && status=4")->count();
            //认筹
            $list[$k]['status5']=M('generate_room')->where("build_id=$v[build_id] && status=5")->count();
            //认购
            $list[$k]['status6']=M('generate_room')->where("build_id=$v[build_id] && status=6")->count();
            //签约
            $list[$k]['status7']=M('generate_room')->where("build_id=$v[build_id] && status=7")->count();
        }

        // $list1=M('generate_room')->where($map)->group('build_id')->field('status, count(*)')->select();
        // echo '<pre>';print_r($list);die;
        $this->assign('list',$list);
        $this->assign('cur','房源统计');
        $this->display();
    }
    //房源详情
    public function statistic_sources_detail(){
        $map['floor_id']=2;
        $map['build_id']=I('build_id');
        $list=M('generate_room')->where($map)->select();
        
        
        $list1=M('generate_room')->where($map)->group('unit')->select();
        foreach($list1 as $k=>$v){
            // $list1[$k]['sub']=M('generate_room')->where("build_id=$v[build_id] && unit=$v[unit]")->group('hierarchy')->field('hierarchy,count(*) cc')->find();
            $sub=M('generate_room')->where("build_id=$v[build_id] && unit=$v[unit]")->group('hierarchy')->field('hierarchy,count(*) cc')->find();
            $list1[$k]['cc']=$sub['cc'];
            for($i=0;$i<$sub['cc'];$i++){
                $list1[$k]['sub'][$i]=0;
            }
        }
        $this->assign('list1',$list1);

        $layer=M('generate_room')->where($map)->group('hierarchy')->select();
        $this->assign('layer',$layer);

        $list2=M('generate_room')->where($map)->group('hierarchy')->select();
        foreach($list2 as $k=>$v){
            $list2[$k]['sub']=M('generate_room')->where("build_id=$v[build_id] && hierarchy=$v[hierarchy]")->select();
            
        }
        $this->assign('list2',$list2);
        // echo '<pre>';print_r($list2);die;



        //待售
        $count1=M('generate_room')->where("build_id=$map[build_id] && status=1")->count();
        //在售
        $count2=M('generate_room')->where("build_id=$map[build_id] && status=2")->count();
        //已售
        $count3=M('generate_room')->where("build_id=$map[build_id] && status=3")->count();
        //预留
        $count4=M('generate_room')->where("build_id=$map[build_id] && status=4")->count();
        //认筹
        $count5=M('generate_room')->where("build_id=$map[build_id] && status=5")->count();
        //认购
        $count6=M('generate_room')->where("build_id=$map[build_id] && status=6")->count();
        //签约
        $count7=M('generate_room')->where("build_id=$map[build_id] && status=7")->count();
        $floor_name=M('floor')->where("id=$map[floor_id]")->getField('name');

        $this->assign('count1',$count1);
        $this->assign('count2',$count2);
        $this->assign('count3',$count3);
        $this->assign('count5',$count5);
        $this->assign('count6',$count6);
        $this->assign('count7',$count7);
        $this->assign('build_name',$map['build_id']);
        $this->assign('floor_name',$floor_name);
        $this->assign('list',$list);
        $this->assign('cur','房源统计');
        $this->display();
    }

    //系统配置
    public function system(){
        //应该是跟着机构走
        $me_id=session('me_id');

        if($_POST){
            $data=$_POST;
            $map=explode(',',$_POST['map']);
            $data['lng']=$map[0];
            $data['lat']=$map[1];
            $in=M('organization_configuration')->where("me_id=$me_id")->find();
            if($in){
                                // print_r($data);die;
                $res=M('organization_configuration')->where("me_id=$me_id")->save($data);
            }else{

                $data['me_id']=$me_id;
                $res=M('organization_configuration')->add($data);
            }
            if($res){
                return $this->success('成功',U('system'));
            }else{
                return $this->error('失败',U('system'));
            }
        }



        $info=M('organization_configuration')->where("me_id=$me_id")->find();
        if($info){
            $info['map']=$info['lng'].','.$info['lat'];
        }
        
        $this->assign('info',$info);
        $this->assign('cur','系统配置');
        $this->display();
    }
    //客户信息配置
    public function system_customer(){
        $me_id=session('me_id');
        if($_POST){
            $data=$_POST;
            $in=M('information_configuration')->where("me_id=$me_id")->find();
            if($in){
                $res=M('information_configuration')->where("me_id=$me_id")->save($data);
            }else{

                $data['me_id']=$me_id;
                $res=M('information_configuration')->add($data);
            }
            if($res){
                return $this->success('成功',U('system_customer'));
            }else{
                return $this->error('失败',U('system_customer'));
            }
        }
        $info=M('information_configuration')->where("me_id=$me_id")->find();
        $this->assign('info',$info);
        $this->assign('cur','客户信息配置');
        $this->display();
    }


        public function uploadImg() {
        if (empty($_FILES) ){
            echo json_encode(['code'=>1,'msg'=>'图片不能为空']);exit;
        }else{
            $file = $_FILES['image'];
            if (!is_dir("Uploads/" . date("Y-m-d"))) {
                mkdir("Uploads/" . date("Y-m-d"));
            }
            if (is_uploaded_file($file['tmp_name'])) {
                $str = stristr($file['name'], '.');
                $str=substr($str,1);
                $avatar_path = "Uploads/" . date("Y-m-d") . "/" . time() . rand(1000,9999) . '.' . $str;

                if(copy($file['tmp_name'],$avatar_path)){
// print_r($file['tmp_name']);die;
                    $game_path = str_replace('Uploads/','game/headimage/',$avatar_path);
                    copy($file['tmp_name'],$game_path);
                    $cover['path'] = "/".$avatar_path;
                    $cover['status'] = 1;
                    $cover['create_time'] = time();
                    $re = M('picture')->add($cover);
                    if($re){
                        $img=get_host() . get_cover($re, "path");
                        $hh['imgurl']=$img;
                        $hh['imgid']=$re;
                        $hh['name']=$file['name'];
                        echo json_encode(['code'=>0,'msg'=>'成功','info'=>$hh]);exit;
                    }else{
                        echo json_encode(['code'=>1,'msg'=>'失败']);exit;
                    }
                }else {
                    echo json_encode(['code'=>1,'msg'=>'失败']);exit;
                }
            }
        }
    }
}